Home All Groups Group Topic Archive Search About

dynamically creating temp tables

Author
3 Nov 2005 9:36 PM
HP
I have a dynamic sql which uses "select into" to create a temp table. It has
to be dynamic because the fieldnames are soft coded.I need to join this temp
table (that i created using the dynamic sql) with another table.Since the
temp table goes out of scope after the exec statement i am not able to use it
in my second query.
Thanks in advance!

Author
3 Nov 2005 9:46 PM
Aaron Bertrand [SQL Server MVP]
Can't you perform the join within the same block of dynamic SQL?



Show quote
"HP" <H*@discussions.microsoft.com> wrote in message
news:BC97CA3E-DFC3-4DB5-AB56-605047D3D527@microsoft.com...
>I have a dynamic sql which uses "select into" to create a temp table. It
>has
> to be dynamic because the fieldnames are soft coded.I need to join this
> temp
> table (that i created using the dynamic sql) with another table.Since the
> temp table goes out of scope after the exec statement i am not able to use
> it
> in my second query.
> Thanks in advance!
Author
3 Nov 2005 10:01 PM
HP
Actually i have to use that temp table in 2 queries.If i include those select
stetements within the same block , the dynamic sql would be big, and the
execution of the dynamic statement could be slow.Correct me if I am wrong.

Show quote
"Aaron Bertrand [SQL Server MVP]" wrote:

> Can't you perform the join within the same block of dynamic SQL?
>
>
>
> "HP" <H*@discussions.microsoft.com> wrote in message
> news:BC97CA3E-DFC3-4DB5-AB56-605047D3D527@microsoft.com...
> >I have a dynamic sql which uses "select into" to create a temp table. It
> >has
> > to be dynamic because the fieldnames are soft coded.I need to join this
> > temp
> > table (that i created using the dynamic sql) with another table.Since the
> > temp table goes out of scope after the exec statement i am not able to use
> > it
> > in my second query.
> > Thanks in advance!
>
>
>
Author
3 Nov 2005 10:06 PM
Aaron Bertrand [SQL Server MVP]
> Actually i have to use that temp table in 2 queries.If i include those
> select
> stetements within the same block , the dynamic sql would be big, and the
> execution of the dynamic statement could be slow.Correct me if I am wrong.

You're using dynamic SQL and #temp tables.  I doubt the size of your dynamic
SQL is going to have a measurable impact on that kind of performance.

If the dynamic SQL is too big for a single varchar(8000) you can always try:

EXEC ( @tempTableCreation +';' + @sqlJoin1 + ';' + @sqlJoin2 )
Author
3 Nov 2005 10:13 PM
HP
My dynamic sql is not too big.If i throw in the other join as you suggested
i'll have 3 select queries.I am basically worried about performance.

Show quote
"Aaron Bertrand [SQL Server MVP]" wrote:

> > Actually i have to use that temp table in 2 queries.If i include those
> > select
> > stetements within the same block , the dynamic sql would be big, and the
> > execution of the dynamic statement could be slow.Correct me if I am wrong.
>
> You're using dynamic SQL and #temp tables.  I doubt the size of your dynamic
> SQL is going to have a measurable impact on that kind of performance.
>
> If the dynamic SQL is too big for a single varchar(8000) you can always try:
>
> EXEC ( @tempTableCreation +';' + @sqlJoin1 + ';' + @sqlJoin2 )
>
>
>
Author
3 Nov 2005 10:39 PM
Aaron Bertrand [SQL Server MVP]
> My dynamic sql is not too big.If i throw in the other join as you
> suggested
> i'll have 3 select queries.I am basically worried about performance.

And I am suggesting that the fact that you already have dynamic SQL and temp
tables, and you have to do the three joins regardless, the difference
between doing the three joins inside of dynamic SQL or not is really going
to have negligible impact on performance, if any.

Of course, the onus is on you to test and make sure the performance is still
acceptable.  In the time you have worried about it and debated with me, you
could have already tested it.  :-(
Author
3 Nov 2005 10:51 PM
ML
If performance is your main worry, then don't use dynamic SQL. Consider the
pros and the cons - if the benefits of using one dynamic procedure outweigh
the performance benefits of using several procedures, then the answer is
quite obvious, isn't it?


ML
Author
3 Nov 2005 11:41 PM
Erland Sommarskog
HP (H*@discussions.microsoft.com) writes:
> My dynamic sql is not too big.If i throw in the other join as you
> suggested i'll have 3 select queries.I am basically worried about
> performance.

I you are creating tables with column names generated at run time,
performance is obviously not your major concern.


--
Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp
Author
3 Nov 2005 9:52 PM
Kalen Delaney
Hi HP

You already have an active thread going on this topic, in this newsgroup;
you do not need to start another one.
Thanks

--
HTH
Kalen Delaney, SQL Server MVP
www.solidqualitylearning.com


Show quote
"HP" <H*@discussions.microsoft.com> wrote in message
news:BC97CA3E-DFC3-4DB5-AB56-605047D3D527@microsoft.com...
>I have a dynamic sql which uses "select into" to create a temp table. It
>has
> to be dynamic because the fieldnames are soft coded.I need to join this
> temp
> table (that i created using the dynamic sql) with another table.Since the
> temp table goes out of scope after the exec statement i am not able to use
> it
> in my second query.
> Thanks in advance!
>

AddThis Social Bookmark Button