Answered by:
Dynamic Data Insert

Question
-
Hi All,
I am trying to insert data into a global temp table from a local temp table dynamically. Since the global temp table need to be appended with a user name towards the end.
Eg: ##List_User1 or ##List_User2
Step 1: Take data from Global Temp table and insert into local temp table
a) Create temp table
b) Insert into temp table
Exec(@InsertGlobalTempSQL_SelectStatement)
Step 2: I am struck here, since local temp table cannot be used into a dynamic sql
I need to refresh the ##List_User1 or User2 table with latest local temp data.
Thanks for your help,
Shreepal
Tuesday, September 5, 2017 12:01 PM
Answers
-
You can use the temp table with dynamic sql after creating the temp table...
this code will not work -
exec ('create table #temp2 (id int)
insert #temp2 values(1)')
select * from #temp2This code will work -
create table #temp1 (id int)
exec ('insert #temp1 values(1)')
select * from #temp1
Please Mark as Answer if my post works for you or Vote as Helpful if it helps you. Kapil Singh
- Proposed as answer by Kapil.Kumawat Tuesday, September 5, 2017 12:12 PM
- Marked as answer by Shreepal Bhandari Wednesday, September 6, 2017 7:31 AM
Tuesday, September 5, 2017 12:12 PM
All replies
-
Looks like pretty wrong approach.....
Sorry cannot test it right now
Exec('create table #tmp (c int);insert into #tmp values (1);'+@InsertGlobalTempSQL_SelectStatement)
Best Regards,Uri Dimant SQL Server MVP, http://sqlblog.com/blogs/uri_dimant/
MS SQL optimization: MS SQL Development and Optimization
MS SQL Consulting: Large scale of database and data cleansing
Remote DBA Services: Improves MS SQL Database Performance
SQL Server Integration Services: Business Intelligence
Tuesday, September 5, 2017 12:06 PMAnswerer -
You can use the temp table with dynamic sql after creating the temp table...
this code will not work -
exec ('create table #temp2 (id int)
insert #temp2 values(1)')
select * from #temp2This code will work -
create table #temp1 (id int)
exec ('insert #temp1 values(1)')
select * from #temp1
Please Mark as Answer if my post works for you or Vote as Helpful if it helps you. Kapil Singh
- Proposed as answer by Kapil.Kumawat Tuesday, September 5, 2017 12:12 PM
- Marked as answer by Shreepal Bhandari Wednesday, September 6, 2017 7:31 AM
Tuesday, September 5, 2017 12:12 PM -
You can also check the below forum for same type of problem -
Please Mark as Answer if my post works for you or Vote as Helpful if it helps you. Kapil Singh
Tuesday, September 5, 2017 12:14 PM -
I am trying to insert data into a global temp table from a local temp table dynamically. Since the global temp table need to be appended with a user name towards the end.
Eg: ##List_User1 or ##List_User2What lead you to make this design decision?
If you tell us the full story of what you are doing, we may be able to help you with a better overall solution.
The problem as described is not that difficult:
CREATE TABLE #temp (...)
EXEC (INSERT #temp(...) SELECT ... FROM ##@globaltemp)But global temp tables with unknown names sounds like a design mistake anyway.
Tuesday, September 5, 2017 12:52 PM