Execute sp_executeSql for select...into #table but Can't Select out Temp Table Data
Local temporary table #table_name
is visible in current session only, global temporary ##table_name
tables are visible in all sessions. Both lives until their session is closed. sp_executesql
- creates its own session (maybe word "scope" would be better) so that's why it happens.
INSERT INTO temporary table from sp_executsql
You can't. There is simply no way to create a #temptable from an EXEC output schema.
INSERT ... EXEC
requires the table to exists (thus must know the schema before execution).
SELECT ... INTO
does not support EXEC as a source.
How can I store a temporary table from an executed procedure?
You can store the data in Global temporary table (##) with Select * into approach and to store in #temp table you have to create the table first which I am aware of while using dynamic sql But you can certainly do that in run time but still you may need some physical table to access it.
create table testtmp (id int, namen varchar(15))
--inserting the data into physical table
insert into testtmp (id, namen)
select 1 as ID, 'XYZ' as namen union all
select 2 as ID, 'ABC' as namen union all
select 3 as ID, 'DIG' as namen
create table #temp (ID int)
declare @sql nvarchar(max) = 'select ID from testtmp'
insert into #temp exec sp_executesql @sql
select * from #temp
Gives you this output:
ID
1
2
3
With global temporary table you can do it easily and you don't have to create any tables, you can specify column names if you would like to.
declare @sql nvarchar(max) = 'select * into ##Gloabltmptest from testtmp'
exec sp_executesql @sql
select * from ##Gloabltmptest
Output:
ID namen
1 XYZ
2 ABC
3 DIG
Added table variable as well, similar to #temp tables.
declare @table table (IDtab int, nametab varchar(15))
declare @sql nvarchar(max) = 'select * from testtmp'
insert into @table exec sp_executesql @sql
select * from @table
Using temp table with exec @sql in stored procedure
#tmpA
is created in a different scope, so is not visible outside of the dynamic SQL. You can just make the ultimate SELECT
a part of the dynamic SQL. Also a couple of other things:
- Always use the schema prefix when creating/referencing objects
- Always use
sp_executesql
for dynamic SQL; in this case it allows you to parameterize the@DRange
value and avoid SQL injection risks. - Always prefix Unicode strings with
N
- Unicode is required forsp_executesql
but if you get lazy about this in other areas of your code it can also lead to painful implicit conversions.
DECLARE @sql NVARCHAR(MAX);
SET @sql = N'select * into #tmpA from dbo.TableA
where create_date >= DATEADD(DAY, -@DRange, GETDATE())
AND is_enabled = 1; SELECT * FROM #tmpA';
EXEC sp_executesql @sql, N'@DRange INT', @DRange;
Of course if all you're doing is selecting, I have a hard time understanding why this is dynamic SQL in the first place. I assume your query (or what you later do with the temp table) is more complicated than this - if so, don't dumb it down for us. Telling us your whole problem will prevent a lot of back and forth, as the additional details could change the answer.
Dynamic SQL results into temp table in SQL Stored procedure
Try:
SELECT into #T1 execute ('execute ' + @SQLString )
And this smells real bad like an sql injection vulnerability.
correction (per @CarpeDiem's comment):
INSERT into #T1 execute ('execute ' + @SQLString )
also, omit the 'execute'
if the sql string is something other than a procedure
Related Topics
How to Combine Results of Two Queries into a Single Dataset
SQL Server Variable Scope in a Stored Procedure
SQL Server Row_Number() on SQL Server 2000
Is There an Oracle Equivalent to SQL Server's Output Inserted.*
Get Unique Values Using String_Agg in SQL Server
How to Create Temp Table with Select * into Temptable from Cte Query
In SQL Server, When Should You Use Go and When Should You Use Semi-Colon ;
Sqlite3 Unique Constraint Failed Error
Truncate Table Within Transaction
Stored Procedure Exec VS Sp_Executesql Difference
Cannot Select from Update Returning Clause in Postgres
How to Dynamically Use Tg_Table_Name in Postgresql 8.2
How to Find Out What Foreign Key Constraint References a Table in SQL Server