使用链接服务器变量将查询转换为动态SQL

时间:2022-10-21 15:44:10

I have a stored procedure that I need to convert so that it reads a linked server variable from a table. From what I know, the only way to do so is to use dynamic SQL. The problem is that I'm failing to convert my query.

我有一个存储过程,我需要转换,以便它从表中读取链接的服务器变量。据我所知,唯一的方法是使用动态SQL。问题是我无法转换我的查询。

Original query:

原始查询:

SET @var1 = ''
SELECT  @var1 = RECEIVER        
FROM    databse1.dbo.table1 
WHERE   SAPNUMBER = @var2

Converted query:

转换查询:

SET @srv = (SELECT server_name
            FROM Configuration.dbo.Server_Switch)

SET @var1 = ''
exec (  
'SELECT ' + @var1 + '= RECEIVER
FROM ' + @srv + '.databse1.dbo.table1 
WHERE SAPNUMBER =' + @var2
)

The error that I'm getting is:

我得到的错误是:

Incorrect syntax near '='

@var1 and @var2 are variables declared beforehand. I'm pretty sure there's a problem in assigning values to these variables in dynamic SQL, hence the '=' error. Can I get some help in converting this query?

@ var1和@ var2是事先声明的变量。我很确定在动态SQL中为这些变量赋值时存在问题,因此出现'='错误。我可以在转换此查询时获得一些帮助吗?

2 个解决方案

#1


4  

One way to solve it is to use sp_executeSql with an output parameter:

解决它的一种方法是使用带有输出参数的sp_executeSql:

SET @srv = (SELECT server_name
            FROM Configuration.dbo.Server_Switch)

DECLARE @sql nvarchar(500),
        @ParmDefinition nvarchar(500);

SET @sql = 
'SELECT @output = RECEIVER
FROM ' + @srv + '.databse1.dbo.table1 
WHERE SAPNUMBER =' + @var2

SET @ParmDefinition = N'@var1 varchar(100) OUTPUT';

EXEC sp_executesql @Sql, @ParmDefinition, @var1=@output OUTPUT;

#2


0  

Please provide more info. We need variables @var2, @var1 declaration, types of variables. If @var2 is varchar, then please update your query in followoing way:

请提供更多信息。我们需要变量@ var2,@ var1声明,变量类型。如果@ var2是varchar,那么请按照以下方式更新您的查询:

declare @sql nvarchar(4000) = 
'SELECT ' + @var1 + '= RECEIVER
FROM ' + @srv + '.databse1.dbo.table1 
WHERE SAPNUMBER =' + char(39) + @var2 + char(39);

exec(@sql); 

P.S. if @var2 numeric:

附: if @ var2 numeric:

declare @sql nvarchar(4000) = 
'SELECT ' + @var1 + '= RECEIVER
FROM ' + @srv + '.databse1.dbo.table1 
WHERE SAPNUMBER =' + @var2;
exec(@sql); 

#1


4  

One way to solve it is to use sp_executeSql with an output parameter:

解决它的一种方法是使用带有输出参数的sp_executeSql:

SET @srv = (SELECT server_name
            FROM Configuration.dbo.Server_Switch)

DECLARE @sql nvarchar(500),
        @ParmDefinition nvarchar(500);

SET @sql = 
'SELECT @output = RECEIVER
FROM ' + @srv + '.databse1.dbo.table1 
WHERE SAPNUMBER =' + @var2

SET @ParmDefinition = N'@var1 varchar(100) OUTPUT';

EXEC sp_executesql @Sql, @ParmDefinition, @var1=@output OUTPUT;

#2


0  

Please provide more info. We need variables @var2, @var1 declaration, types of variables. If @var2 is varchar, then please update your query in followoing way:

请提供更多信息。我们需要变量@ var2,@ var1声明,变量类型。如果@ var2是varchar,那么请按照以下方式更新您的查询:

declare @sql nvarchar(4000) = 
'SELECT ' + @var1 + '= RECEIVER
FROM ' + @srv + '.databse1.dbo.table1 
WHERE SAPNUMBER =' + char(39) + @var2 + char(39);

exec(@sql); 

P.S. if @var2 numeric:

附: if @ var2 numeric:

declare @sql nvarchar(4000) = 
'SELECT ' + @var1 + '= RECEIVER
FROM ' + @srv + '.databse1.dbo.table1 
WHERE SAPNUMBER =' + @var2;
exec(@sql);