title: How to get return value from EXEC sp_executesql
body: <p>How do I get the return value from EXEC sp_executesql @OpenQry so I can check if the value exists in IF EXISTS?</p>
  
  <pre><code>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 TableName=@TableName)
  
  FETCH NEXT FROM CUR_QRY INTO @TableName
  END
  CLOSE CUR_QRY
  DEALLOCATE CUR_QRY
  </code></pre>