我希望将链接服务器用作OPENQUERY
中的变量。我如何使它工作?
SET @LinkedServer = 'MyLinkedServer';
INSERT INTO TABLE_2 (Product, Name, State)
SELECT *
FROM OPENQUERY ('+@LinkedServer+', 'SELECT Product, Name, ''IL'' FROM DBNAME.TABLE_1);
答案 0 :(得分:0)
这样的事情(答案来自here):
DECLARE @OPENQUERY nvarchar(4000), @TSQL nvarchar(4000), @LinkedServer nvarchar(4000)
SET @LinkedServer = 'MyLinkedServer'
SET @OPENQUERY = 'SELECT * FROM OPENQUERY('+ @LinkedServer + ','''
SET @TSQL = 'SELECT Product, Name, ''IL'' FROM DBNAME.TABLE_1'')' +
INSERT INTO TABLE_2 (Product, Name, State)
EXEC (@OPENQUERY+@TSQL)
答案 1 :(得分:0)
@Kamran,感谢您提供的信息。
我已经制定了如下解决方案:
SET @TSQL = N'INSERT INTO TABLE_2 (产品,名称,国家) SELECT * FROM OPENQUERY('+ @ linkedServer +','SELECT Product,Name,''''IL''''FROM DBNAME.TABLE_1);'