如何從EXEC sp_executesql @OpenQry獲取返回值,以便我可以檢查IF EXISTS中是否存在該值?如何從EXEC獲取返回值sp_executesql
DECLARE @TableName VARCHAR(25)
DECLARE @TD_QUERY NVARCHAR(MAX)
DECLARE CUR_QRY CURSOR FOR
SELECT TABLENAME FROM dbo.tbl_table
OPEN CUR_QRY
FETCH NEXT FROM CUR_QRY INTO @TableName
WHILE @@FETCH_STATUS = 0
BEGIN
SET @OpenQry = 'SELECT * FROM OPENQUERY(linkedserver,''SELECT TABLENAME FROM DBC.TABLES WHERE TABLEKIND=''''T'''' AND DATABASENAME=''''dbname'''' AND TABLENAME=''''' + @TableName + ''''''')'
EXEC sp_executesql @OpenQry
IF EXISTS (SELECT @OpenQry)
AND EXISTS (SELECT TableName FROM dbo.table WHERE [email protected])
FETCH NEXT FROM CUR_QRY INTO @TableName
END
CLOSE CUR_QRY
DEALLOCATE CUR_QRY
你將不得不把你的動態sql的結果放到一個表(持久性,溫度或變量)中。是否有一個原因,你不能只是查詢鏈接服務器?這種光標方法將會讓你的表演失去生命。 –
[如何將sp \ _executesql結果導入變量?](http://stackoverflow.com/questions/803211/how-to-get-sp-executesql-result-into-a-variable) – DeanOC