I'm trying to use variables
from different databases (hier called Database1 and Database2) and to execute sp_executesql
statement.
USE Database1
DECLARE @testVariable BIT = 1
USE Database2
DECLARE @Param1 t_ID
DECLARE @Param2 t_ID
DECLARE @Param3 VARCHAR(100) = '01.01.2010'
DECLARE @Param4 VARCHAR(100) = '31.12.2015'
DECLARE @Param5 VARCHAR(100) = '1'
DECLARE @Param6 VARCHAR(100) = 'EUR'
DECLARE @ParamDefinition NVARCHAR(1000) = ''
DECLARE @SQLString NVARCHAR(1000) = ''
DECLARE @SQLObject VARCHAR(100) = ''
INSERT INTO @Param1 VALUES (1) ,(345), (123)
INSERT INTO @Param2 VALUES (106)
SET @SQLObject = 'Database2.dbo.Testfunction2'
SET @SQLString = 'SELECT * FROM ' + @SQLObject + '(@Param1, @Param2, @Param3, @Param4, @Param5, @Param6)'
SET @ParamDefinition = '@Param1 t_ID READONLY, @Param2 t_ID READONLY, @Param3 VARCHAR(100), @Param4 VARCHAR(100), @Param5 VARCHAR(100), @Param6 VARCHAR(100)'
IF @testVariable = 1
BEGIN
EXECUTE sp_executesql @SQLString, @ParamDefinition, @Param1, @Param2, @Param3, @Param4, @Param5, @Param6
END
In mysp_executesql
statement I have also two Variables with user defined table types @Param1 t_ID
and @Param2 t_ID
. This Type is created in both Databases.
If I comment --USE Database1
and run the code above, so that I stay within only one DB (Database2) for the whole process, there is no error,
but when I try to run the same procedure with using of two Databases: USE Database1
and USE Database2
, I get a compatibility error:
Msg 206, Level 16, State 2, Procedure PROC_Test, Line 0
Operand type clash: t_ID is incompatible with t_ID
What am I doing wrong? Is there any problem solving?