SQL Server 2014 - sp_describe_cursor_column 动态查询问题

SQL Server 2014 - sp_describe_cursor_column for dynamic queries issue

基本上我想知道 columns/aliases 的动态查询结果集。我尝试使用 sp_describe_cursor_column 但没有成功。

我 returns 光标没有退出。但是我可以从这样的游标中获取值...

密码是:

ALTER PROC TestProc
AS
       DECLARE @dynamicSQL nvarchar(200)
       -- Have code that will construct the dynamic SQL
   SET @dynamicSQL = '  select table_name, TABLE_TYPE from INFORMATION_SCHEMA.TABLES'

   -- The cursor that will be filled by the dynamic SQL
   DECLARE @outputCursor CURSOR
   -- Create the dynamic SQL to fill a CURSOR instead
   SET @dynamicSQL = 'SET @outputCursor = CURSOR FORWARD_ONLY STATIC FOR ' +
          @dynamicSQL + ' ; OPEN @outputCursor'

   -- Execute dynamic sql
   exec sp_executesql                  -- sp_executesql will essentially create a sproc
          @dynamicSQL,                 -- The SQL statement to execute (body of sproc)
          N'@outputCursor CURSOR OUTPUT', -- The parameter list for the sproc: OUTPUT CURSOR
          @outputCursor OUTPUT         -- The parameter to pass to the sproc: the CURSOR


declare @Report cursor 

 exEC sp_describe_cursor_columns
    @cursor_return = @Report OUTPUT
    ,@cursor_source = N'local' 
    ,@cursor_identity = N'outputCursor';


   -- Code that will just output the values from the cursor
   DECLARE @tableName nvarchar(200), @table_type nvarchar(200);
   FETCH NEXT FROM @outputCursor INTO @tableName, @table_type

   -- Loop while there're more things in the cursor
   WHILE @@FETCH_STATUS = 0
   BEGIN
          PRINT @tableName
          FETCH NEXT FROM @outputCursor INTO @tableName, @table_type
   END

   -- Be nice, close & deallocate cursor
   CLOSE @outputCursor
   DEALLOCATE @outputCursor

这是结果:

Msg 16916, Level 16, State 4, Procedure sp_describe_cursor_columns, Line 23 A cursor with the name 'outputCursor' does not exist. DATABASE_UPDATE SYSTEM_CONFIGURATION ....

结果我想看table_name,table_type。 不要告诉我我可以从字符串中提取它,因为用户可能会发送 select * from xxxx.

我找到了一些其他方法来提取动态查询结果集的描述。

declare @Table nvarchar(200) ;
DECLARE @dynamicSQL nvarchar(200)
SET @dynamicSQL = 'select table_name, TABLE_TYPE from INFORMATION_SCHEMA.TABLES'


SELECT  @Table = COALESCE(@Table + ', ', '') + Name
FROM sys.dm_exec_describe_first_result_set
        (@dynamicSQL, NULL,1)

print @Table