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

前端 未结 3 1900
孤街浪徒
孤街浪徒 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

    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 + ']')
    

提交回复
热议问题