Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Dynamic SQL Question

Author  Topic 

mattt
Posting Yak Master

194 Posts

Posted - 2008-06-04 : 05:34:21
Hi,

I have a table with contains - amonst other things - a varchar field in which there is a SELECT statement. These SELECT statements invariably start with SELECT TOP 50 but thereafter diverge - and some of them bring back quite a lot of columns.

I'm trying to write an SP which will page through each of these statements in turn, execute the SQL into a temporary table and then count the rows in that table.

I'm perplexed as to how to do this because AFAIK you can't do this:
#MyTempTable = exec @strSQL
And indeed you'd need to define #MyTempTable beforehand for this to work, and I can't actually do that because the columns coming back from each query may be subtly different.

The only way I can think of to solve this is to somehow modify each statement from a SELECT TOP 50 to a SELECT TOP 50 INTO ... but seeing as how the statements are different that could be a right royal pain in the arse.

Is there another potential solution that I'm missing here?
Cheers,
Matt

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-06-04 : 05:38:26
This can help you:http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=85347

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

ganeshkumar08
Posting Yak Master

187 Posts

Posted - 2008-06-04 : 06:40:09
As per my understanding to define a table with required columns try below one.

Select * into #TempTable From
(Select Col1,Col2, Col3 From YourTable Where 1=2) a

The Above sql statement will create a table without data.
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-06-04 : 06:46:17
ganeshkumar08,

You missed a point here. The OP wants to base his temp table on the output of dynamic sql.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page
   

- Advertisement -