Home All Groups Group Topic Archive Search About
Author
3 Nov 2005 5:36 PM
HP
I am trying to use "select into" query in a dynamic sql.But the temp table is
not getting created when i execute the dynamic sql.What am I missing?

Thanks in advance.

Author
3 Nov 2005 6:24 PM
Bob Barrows [MVP]
HP wrote:
> I am trying to use "select into" query in a dynamic sql.But the temp
> table is not getting created when i execute the dynamic sql.What am I
> missing?
>
Probably the fact that the dynamic sql is executed in a separate process:
one of the many reasons to avoid dynamic sql.

See http://www.sommarskog.se/dynamic_sql.html for more information. There
are several other informative articles on that site that are well worth
reading.

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Author
3 Nov 2005 7:11 PM
HP
Any more suggestions please?

Show quote
"Bob Barrows [MVP]" wrote:

> HP wrote:
> > I am trying to use "select into" query in a dynamic sql.But the temp
> > table is not getting created when i execute the dynamic sql.What am I
> > missing?
> >
> Probably the fact that the dynamic sql is executed in a separate process:
> one of the many reasons to avoid dynamic sql.
>
> See http://www.sommarskog.se/dynamic_sql.html for more information. There
> are several other informative articles on that site that are well worth
> reading.
>
> --
> Microsoft MVP -- ASP/ASP.NET
> Please reply to the newsgroup. The email account listed in my From
> header is my spam trap, so I don't check it very often. You will get a
> quicker response by posting to the newsgroup.
>
>
>
Author
3 Nov 2005 7:48 PM
Kalen Delaney
Hi HP

What didn't you like about Bob's suggestion? Did you follow the link he
provided?

I bet you'd get more suggestions if you would provide us with more
information. For the tiny bit of info you gave us, Bob's suggestions was
spot on, and I doubt anyone could do any better.

What version are you running? How are you creating and running the dynamic
SQL? What does it contain?
How do you know the temp table isn't created? Is it just that it isn't
available when the dynamic SQL is done? That doesn't mean it wasn't creating
while the dynamic SQL was executing.

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


Show quote
"HP" <H*@discussions.microsoft.com> wrote in message
news:11A865F8-F55E-46C0-80B9-FD194F5E79E7@microsoft.com...
> Any more suggestions please?
>
> "Bob Barrows [MVP]" wrote:
>
>> HP wrote:
>> > I am trying to use "select into" query in a dynamic sql.But the temp
>> > table is not getting created when i execute the dynamic sql.What am I
>> > missing?
>> >
>> Probably the fact that the dynamic sql is executed in a separate process:
>> one of the many reasons to avoid dynamic sql.
>>
>> See http://www.sommarskog.se/dynamic_sql.html for more information. There
>> are several other informative articles on that site that are well worth
>> reading.
>>
>> --
>> Microsoft MVP -- ASP/ASP.NET
>> Please reply to the newsgroup. The email account listed in my From
>> header is my spam trap, so I don't check it very often. You will get a
>> quicker response by posting to the newsgroup.
>>
>>
>>
>
Author
3 Nov 2005 7:58 PM
Bob Barrows [MVP]
Kalen beat me to it.
I can't offer any suggestions without knowing what you are trying to
accomplish
..
HP wrote:
Show quote
> Any more suggestions please?
>
> "Bob Barrows [MVP]" wrote:
>
>> HP wrote:
>>> I am trying to use "select into" query in a dynamic sql.But the temp
>>> table is not getting created when i execute the dynamic sql.What am
>>> I missing?
>>>
>> Probably the fact that the dynamic sql is executed in a separate
>> process: one of the many reasons to avoid dynamic sql.
>>
>> See http://www.sommarskog.se/dynamic_sql.html for more information.
>> There are several other informative articles on that site that are
>> well worth reading.
>>


--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Author
3 Nov 2005 8:16 PM
HP
Sorry for not giving you a detailed explanation of what i am trying to achieve.
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.

Show quote
"Bob Barrows [MVP]" wrote:

> Kalen beat me to it.
> I can't offer any suggestions without knowing what you are trying to
> accomplish
> ..
> HP wrote:
> > Any more suggestions please?
> >
> > "Bob Barrows [MVP]" wrote:
> >
> >> HP wrote:
> >>> I am trying to use "select into" query in a dynamic sql.But the temp
> >>> table is not getting created when i execute the dynamic sql.What am
> >>> I missing?
> >>>
> >> Probably the fact that the dynamic sql is executed in a separate
> >> process: one of the many reasons to avoid dynamic sql.
> >>
> >> See http://www.sommarskog.se/dynamic_sql.html for more information.
> >> There are several other informative articles on that site that are
> >> well worth reading.
> >>
>
>
> --
> Microsoft MVP -- ASP/ASP.NET
> Please reply to the newsgroup. The email account listed in my From
> header is my spam trap, so I don't check it very often. You will get a
> quicker response by posting to the newsgroup.
>
>
>
Author
3 Nov 2005 8:42 PM
Bob Barrows [MVP]
HP wrote:
> Sorry for not giving you a detailed explanation of what i am trying
> to achieve. I have a dynamic sql which uses "select into" to create a
> temp table.

Yes, I knew that. What I don't know is "why are you doing this"? What are
you trying to achieve?

> It has to be dynamic because the fieldnames are soft
> coded.

Do they have to be? Why do they have to be dynamic? If we knew what your
objective was, we might be able to suggest an alternative.


Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Author
3 Nov 2005 8:12 PM
James Ma
I guess the problem is like following

exec('select 20 as tmp_id into #tmp')
select * from #tmp  -- This simply can't see the #tmp table

James

Show quote
"HP" wrote:

> Any more suggestions please?
>
> "Bob Barrows [MVP]" wrote:
>
> > HP wrote:
> > > I am trying to use "select into" query in a dynamic sql.But the temp
> > > table is not getting created when i execute the dynamic sql.What am I
> > > missing?
> > >
> > Probably the fact that the dynamic sql is executed in a separate process:
> > one of the many reasons to avoid dynamic sql.
> >
> > See http://www.sommarskog.se/dynamic_sql.html for more information. There
> > are several other informative articles on that site that are well worth
> > reading.
> >
> > --
> > Microsoft MVP -- ASP/ASP.NET
> > Please reply to the newsgroup. The email account listed in my From
> > header is my spam trap, so I don't check it very often. You will get a
> > quicker response by posting to the newsgroup.
> >
> >
> >
Author
3 Nov 2005 8:20 PM
Bob Barrows [MVP]
Good guess. You may be correct, but: that does not tell us _why_ he is
trying to do this. Knowing what his real purpose is will help us suggest
alternatives.

James Ma wrote:
Show quote
> I guess the problem is like following
>
> exec('select 20 as tmp_id into #tmp')
> select * from #tmp  -- This simply can't see the #tmp table
>
> James
>
> "HP" wrote:
>
>> Any more suggestions please?
>>
>> "Bob Barrows [MVP]" wrote:
>>
>>> HP wrote:
>>>> I am trying to use "select into" query in a dynamic sql.But the
>>>> temp table is not getting created when i execute the dynamic
>>>> sql.What am I missing?
>>>>
>>> Probably the fact that the dynamic sql is executed in a separate
>>> process: one of the many reasons to avoid dynamic sql.
>>>
>>> See http://www.sommarskog.se/dynamic_sql.html for more information.
>>> There are several other informative articles on that site that are
>>> well worth reading.
>>>
>>> --
>>> Microsoft MVP -- ASP/ASP.NET
>>> Please reply to the newsgroup. The email account listed in my From
>>> header is my spam trap, so I don't check it very often. You will
>>> get a quicker response by posting to the newsgroup.

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Author
3 Nov 2005 8:32 PM
HP
Can you pls refer to my previous reply.I've explained my problem.
Thanks!Your help would be greatly appreciated.

Show quote
"Bob Barrows [MVP]" wrote:

> Good guess. You may be correct, but: that does not tell us _why_ he is
> trying to do this. Knowing what his real purpose is will help us suggest
> alternatives.
>
> James Ma wrote:
> > I guess the problem is like following
> >
> > exec('select 20 as tmp_id into #tmp')
> > select * from #tmp  -- This simply can't see the #tmp table
> >
> > James
> >
> > "HP" wrote:
> >
> >> Any more suggestions please?
> >>
> >> "Bob Barrows [MVP]" wrote:
> >>
> >>> HP wrote:
> >>>> I am trying to use "select into" query in a dynamic sql.But the
> >>>> temp table is not getting created when i execute the dynamic
> >>>> sql.What am I missing?
> >>>>
> >>> Probably the fact that the dynamic sql is executed in a separate
> >>> process: one of the many reasons to avoid dynamic sql.
> >>>
> >>> See http://www.sommarskog.se/dynamic_sql.html for more information.
> >>> There are several other informative articles on that site that are
> >>> well worth reading.
> >>>
> >>> --
> >>> Microsoft MVP -- ASP/ASP.NET
> >>> Please reply to the newsgroup. The email account listed in my From
> >>> header is my spam trap, so I don't check it very often. You will
> >>> get a quicker response by posting to the newsgroup.
>
> --
> Microsoft MVP -- ASP/ASP.NET
> Please reply to the newsgroup. The email account listed in my From
> header is my spam trap, so I don't check it very often. You will get a
> quicker response by posting to the newsgroup.
>
>
>
Author
3 Nov 2005 6:25 PM
John Bell
Hi

Without any code it is very difficult to recommend anything.

You may have to watch that the temporary table does not go out of scope. Try
creating the table outside the dynamic SQL and then use INSERT.

John

Show quote
"HP" wrote:

> I am trying to use "select into" query in a dynamic sql.But the temp table is
> not getting created when i execute the dynamic sql.What am I missing?
>
> Thanks in advance.
Author
3 Nov 2005 6:31 PM
HP
Thanks for your reply.I figured scope was the problem. I can't create the
table outside because the field names are soft coded.Can you pls suggest
something?
Thanks again!
Show quote
"John Bell" wrote:

> Hi
>
> Without any code it is very difficult to recommend anything.
>
> You may have to watch that the temporary table does not go out of scope. Try
> creating the table outside the dynamic SQL and then use INSERT.
>
> John
>
> "HP" wrote:
>
> > I am trying to use "select into" query in a dynamic sql.But the temp table is
> > not getting created when i execute the dynamic sql.What am I missing?
> >
> > Thanks in advance.
Author
3 Nov 2005 6:44 PM
James Ma
Try to use fake temp table in a specific user database (set it to use simple
recovery mode) instead of real temp table in the tempdb. In that way you can
avoid the scope problem.

James

Show quote
"HP" wrote:

> Thanks for your reply.I figured scope was the problem. I can't create the
> table outside because the field names are soft coded.Can you pls suggest
> something?
> Thanks again!
> "John Bell" wrote:
>
> > Hi
> >
> > Without any code it is very difficult to recommend anything.
> >
> > You may have to watch that the temporary table does not go out of scope. Try
> > creating the table outside the dynamic SQL and then use INSERT.
> >
> > John
> >
> > "HP" wrote:
> >
> > > I am trying to use "select into" query in a dynamic sql.But the temp table is
> > > not getting created when i execute the dynamic sql.What am I missing?
> > >
> > > Thanks in advance.
Author
3 Nov 2005 7:49 PM
John Bell
Hi

You are going to have to put most code into the one execute statement, as
this is probably going to get messy you should probably re-think what you are
trying to achieve and why it is such a pain to implement.

Without seeing actual code it is hard to suggest anything else.

John

Show quote
"HP" wrote:

> Thanks for your reply.I figured scope was the problem. I can't create the
> table outside because the field names are soft coded.Can you pls suggest
> something?
> Thanks again!
> "John Bell" wrote:
>
> > Hi
> >
> > Without any code it is very difficult to recommend anything.
> >
> > You may have to watch that the temporary table does not go out of scope. Try
> > creating the table outside the dynamic SQL and then use INSERT.
> >
> > John
> >
> > "HP" wrote:
> >
> > > I am trying to use "select into" query in a dynamic sql.But the temp table is
> > > not getting created when i execute the dynamic sql.What am I missing?
> > >
> > > Thanks in advance.
Author
3 Nov 2005 11:45 PM
Erland Sommarskog
HP (H*@discussions.microsoft.com) writes:
> Thanks for your reply.I figured scope was the problem. I can't create the
> table outside because the field names are soft coded.Can you pls suggest
> something?

You will have to run everything within that dynamic SQL.

Now, what everyone really are asking you for, is to tell us what your
real business problem is. That is, why do you need those soft-coded
column names?

As a completely wild guess, you are into dynamic crosstabs. In such
case, stroll over to http://www.rac4sql.net for a third-party product
that may be useful to you. (Never used it myself, but I've seen people
expressed their joy over RAC.)


--
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
4 Nov 2005 1:51 PM
Tony Rogerson
If you aren't bothered about multi connections using this then use a global
temporary table...

exec( 'select * into ##tmp from sysobjects' )
select * from ##tmp

Another approach is to use a global temp table suffixed by your spid...

declare @sql
set @sql = 'select * into ##tmp' + cast( @@spid as varchar(10) ) + ' from
sysobjects'
exec( @sql )

set @sql = 'select * from ##tmp' + cast( @@spid as varchar(10) )
exec( @sql )

Or, consider using a permanent table instead rather than a # or ## table.

Tony

--
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials


Show quote
"HP" <H*@discussions.microsoft.com> wrote in message
news:83CDC32F-A76E-462B-AA74-2C2D0807861F@microsoft.com...
>I am trying to use "select into" query in a dynamic sql.But the temp table
>is
> not getting created when i execute the dynamic sql.What am I missing?
>
> Thanks in advance.
Author
4 Nov 2005 2:21 PM
Joe
This is bad for many reasons but you could use a global temp table.

Declare @strSQL varchar(8000)
set @strSQL =
    'Select * into ##temptable from northwind..orders'
exec (@strSQL)
Select * from ##temptable

I would encourage you to look for alternatives, but this will do what you
are asking.


Show quote
"HP" <H*@discussions.microsoft.com> wrote in message
news:83CDC32F-A76E-462B-AA74-2C2D0807861F@microsoft.com...
>I am trying to use "select into" query in a dynamic sql.But the temp table
>is
> not getting created when i execute the dynamic sql.What am I missing?
>
> Thanks in advance.

AddThis Social Bookmark Button