Author |
Topic |
Zifter
Yak Posting Veteran
51 Posts |
Posted - 2011-12-01 : 08:44:03
|
I've been reading a lot about temporary tables and dynamic sql, but I can't seem to find a solution that suits my needs.The problem is that I don't know in front which columns the dynamic sql is going to return, so I can't define the table.I would like to do something like thisdeclare @sql varchar(max);select @sql = 'select * from MyTable'insert into #tmptable exec(@sql)select * from #tmptable Any suggestions? |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-12-01 : 08:51:55
|
Exactly that almostYou have to create the temp table first.Could also use a table variable.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
Zifter
Yak Posting Veteran
51 Posts |
Posted - 2011-12-01 : 09:01:08
|
That is where the problem lies.I don't know which and how many columns my dynamic sql is going to return, so I don't know how to create the temporary table (or table variable) |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-01 : 09:32:32
|
quote: Originally posted by Zifter That is where the problem lies.I don't know which and how many columns my dynamic sql is going to return, so I don't know how to create the temporary table (or table variable)
then why not create it on the fly using SELECT .....INTO syntax?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-12-01 : 09:38:37
|
You can use openrowset or probably a linked server with a select into - but that will create a new connection.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
Zifter
Yak Posting Veteran
51 Posts |
Posted - 2011-12-01 : 09:43:07
|
visakh16, if I understand you correctly, you suggest to include the "into #tmptable" in the dynamic sql. Something like this
declare @sql varchar(max)select @sql = 'select * into #tmptable from MyTable; select * from #tmptable'exec(@sql) Correct? But then the #tmptable is only known within the scope of the dynamic sql. I would prefer to have access to the temporary table outside of the dynamic sql.The dynamic sql is rather complex and I need to use certain returned values later on.I *could* try to put all of my logic in one big dynamic sql. But I was hoping to avoid that, improving the readablity... |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-01 : 09:45:30
|
quote: Originally posted by Zifter visakh16, if I understand you correctly, you suggest to include the "into #tmptable" in the dynamic sql. Something like this
declare @sql varchar(max)select @sql = 'select * into #tmptable from MyTable; select * from #tmptable'exec(@sql) Correct? But then the #tmptable is only know within the scope of the dynamic sql. I would prefer to have access to the temporary table outside of the dynamic sql.The dynamic sql is rather complex and I need to use certain returned values later on.I *could* try to put all of my logic in one big dynamic sql. But I was hoping to avoid that, improving the readablity...
if you want it afterwards outside why not create ## tables or permanent tables inside dynamic sql unless you've this executed concurrently by more than 1 sessions------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
Zifter
Yak Posting Veteran
51 Posts |
Posted - 2011-12-01 : 09:47:43
|
quote: Originally posted by nigelrivett You can use openrowset or probably a linked server with a select into - but that will create a new connection.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy.
I tried using openrowset, but i get the following error"Deferred prepare could not be completed" |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-12-01 : 09:49:12
|
See my previous post.Other options arePut all of your code inside a dynamic sql string.Create the temp table using the format created by the query - could create a temp table in the dynamic sql block and return the format from information_schema then use that to cretae a temp table - would need to create a table then alter it to add the columns dynamically. Then you can insert into it using dynamic sql. All access to the temp table would need to be via dynamic sql as the sp wouldn't have the format.You're probably getting the idea that this isn't a good way to go.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
Zifter
Yak Posting Veteran
51 Posts |
Posted - 2011-12-01 : 09:50:25
|
quote: if you want it afterwards outside why not create ## tables or permanent tables inside dynamic sql unless you've this executed concurrently by more than 1 sessions
There is a chance that multiple users will run it at the same time, so I can't use global temporary tables.I was hoping that sql server would create the temporary table with the necessary columns on the fly, depending on the query.I looks like I'll have to make one big dynamic sql and execute it... |
|
|
Zifter
Yak Posting Veteran
51 Posts |
Posted - 2011-12-01 : 09:54:28
|
quote: Originally posted by nigelrivett See my previous post.Other options arePut all of your code inside a dynamic sql string.Create the temp table using the format created by the query - could create a temp table in the dynamic sql block and return the format from information_schema then use that to cretae a temp table - would need to create a table then alter it to add the columns dynamically. Then you can insert into it using dynamic sql. All access to the temp table would need to be via dynamic sql as the sp wouldn't have the format.You're probably getting the idea that this isn't a good way to go.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy.
That was exactly what i was trying to avoid doing...Thanks anyway to both of you for all the help!Here I go creating a huge, hard to read dynamic sql... |
|
|
|