How to access dataset in current scope generated by a call to a stored procedure in TSQL?

前端 未结 3 1907
孤街浪徒
孤街浪徒 2021-02-15 17:29

Problem Background

Generating and accessing data of a fixed column layout is easy. You can create local temp tables up-front, and populate them by calling stored proc

相关标签:
3条回答
  • 2021-02-15 18:22

    Create the temp table before the sp_executesql: it will still be in scope for "inner" scopes like the sp_executesql

    Change the SQL to do an INSERT rather than SELECT..INTO...

    Edit:

    Make the table wide enough to cover all options.

    Frankly, SQL is designed to work with fixed table definitions: variable output signatures (tables) leads to the problem you have...

    0 讨论(0)
  • 2021-02-15 18:22

    So, I am copying my answer from Insert results of a stored procedure into a temporary table. Hope it helps. Yes, it's global temporary table again and the only different is the GUID appended there.


    I met the same problem and here is what I did for this from Paul's suggestion. The main part is here is to use NEWID() to avoid multiple users run the store procedures/scripts at the same time, the pain for global temporary table.

    DECLARE @sql varchar(max) = '', 
    @tmp_global_table varchar(255) = '##global_tmp_' + CONVERT(varchar(36), NEWID())
    SET @sql = @sql + 'select * into [' + @tmp_global_table + '] from YOURTABLE'
    EXEC(@sql)
    
    EXEC('SELECT * FROM [' + @tmp_global_table + ']')
    
    0 讨论(0)
  • 2021-02-15 18:31

    One clunky but possible option is to create a single-column table in the caller, then alter it in the callee. This gets around the scope issue but makes things quite a bit harder...

    If you build a stored procedure that accepts input and output table names, which makes the second table look exactly like the first by dropping and adding columns, you might have a more general solution.

    Selecting from tempdb.INFORMATION_SCHEMA.COLUMNS will let you find the column information about any temp table (which you can detect by whether the table name starts with #). Here's an example:

    CREATE TABLE #blah (a int)
    SELECT *
    FROM tempdb.INFORMATION_SCHEMA.COLUMNS
    WHERE Object_id('tempdb.dbo.' + TABLE_NAME) = Object_id('tempdb.dbo.#blah')
    DROP TABLE #blah
    

    Note that the table name in the view is not #blah (it will probably be something like #blah___{lots of underscores}___00000000021D thus the use of Object_id() to correlate the two.

    To use it practically, instead of filling the first table with data, then morphing the second table and copying the data into it, I would suggest creating an empty table first by running your process with an added TOP 0 or WHERE 1 = 0, then copying the table's structure to the correct one with your table-copying SP, then running the data process for real to insert only once to the correct table.

    Like I said, clunky, but could be useful in some narrow situations where no other options are available.

    0 讨论(0)
提交回复
热议问题