all groups > sql server programming > july 2006 >
You're in the

sql server programming

group:

#temporary table in EXEC


#temporary table in EXEC Rad
7/31/2006 11:43:39 PM
sql server programming:
In my stored procedure I need to put data I get from dynamically built
SELECT query into particular temporary table.
i.e.
EXEC ('SELECT 1 AS [number] INTO #t')
SELECT * FROM #t
Of course this doesn't work because of scope of [#t]
Unfortunatelly I don't know exact structure of [#t] in advance, so I
can't create it explicitly.
Any ideas?
Re: #temporary table in EXEC Uri Dimant
8/1/2006 12:00:00 AM
Rad
SELECT * INTO #t FROM what?



[quoted text, click to view]

Re: #temporary table in EXEC Immy
8/1/2006 12:00:00 AM
You haven't defined where you want to select from. If you do that, it should
work.

[quoted text, click to view]

Re: #temporary table in EXEC Immy
8/1/2006 12:00:00 AM
But you would need to encapsulate the select statement within the exec
too... eg.

EXEC ('SELECT 1 AS [number] INTO #t from TABLE
SELECT * FROM #t')

[quoted text, click to view]

Re: #temporary table in EXEC Erland Sommarskog
8/1/2006 12:00:00 AM
Rad (lytchev@gmail.com) writes:
[quoted text, click to view]

Yes, back out and look for an entirely different solution.

The precise reason the above fails is that a temp table disappears when
the scope it was created in exits. You can work around this with a
permanent table or a global temp table (one with two ##). But then you
must take precautions that the name does not collide between parallel
invocations of the code.

And furthermore, all your further work with this table will be through
dynamic SQL, and it will be extremely painful. You are really a lot lot
better off doing this exercise from a client language, which is far
more apt at string manipulation than T-SQL.

And, of course, the fact that you don't know your table structure
in advance is highly suspicious. What are you really trying to accomplish?
The one case I can think of is a dynamic crosstab. In such case, look
at http://www.rac4sql.net/ for a third-party product that can save you
a lot of headache.



--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
Re: #temporary table in EXEC Uri Dimant
8/1/2006 12:00:00 AM
Rad
As you said you do not know how many columns you are getting from
very-long-dynamically-built-statement, am I right?

In that case use SELECT * INTO #Test FROM
very-long-dynamically-built-statement

Now that you have created a new temporary table you may alter the table and
add another column as SUSER_SID() by deafult

I hope you take care of preventing from sql injection afftected your
database








[quoted text, click to view]

Re: #temporary table in EXEC Erland Sommarskog
8/1/2006 12:00:00 AM
Omnibuzz (Omnibuzz@discussions.microsoft.com) writes:
[quoted text, click to view]

Fool-proof for a unique name? Yss, it should br safe. Even

SELECT @tblname = #mytemp + ltrim(str(@@spid))

should do.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
Re: #temporary table in EXEC Arnie Rowland
8/1/2006 12:05:54 AM
Regular table, global ##Temp table. -Both subject to interference from other
users.

IF you could define the #Temp table [#t] before calling the dynamic sql, you
could capture the output. But with your specification, it is not possible to
use a local #Temp table.

--
Arnie Rowland, Ph.D.
Westwood Consulting, Inc

Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous


[quoted text, click to view]

Re: #temporary table in EXEC Rad
8/1/2006 12:15:39 AM
[quoted text, click to view]
SELECT * INTO #t FROM [very-long-dynamically-built-statement]

Let's say

CREATE PROCEDURE [sp_select]
@nStyle int = 0
AS
BEGIN
DECLARE @cQuery nvarchar(1000)
IF (@nStyle = 0)
SET @cQuery = 'SELECT SUSER_SNAME()'
ELSE
SET @cQuery = 'SELECT SUSER_SID()'

SET @cQuery = @cQuery + ' AS [column] INTO #t'

IF (OBJECT_ID('tempdb..#t') IS NOT NULL) OR (OBJECT_ID('#t') IS NOT
NULL)
DROP TABLE #t
EXEC @cQuery
SELECT * FROM #t
END
GO

And I can't just "SELECT" because client-side application may put some
values into the same [#t] table and call another procedure to save that
values into actual tables.

Kind of Enterprise Engine, you know :)

[quoted text, click to view]
RE: #temporary table in EXEC Omnibuzz
8/1/2006 12:16:01 AM
As you probably know, Your dynamic query runs in its own scope and is
different from the scope where you try to select from the temporary table.
So, this possible only if you know there will be no concurrent execution of
this SP, in that case you can go for a global temp table. If not I guess its
a dead end.
--
-Omnibuzz (The SQL GC)

http://omnibuzz-sql.blogspot.com/



[quoted text, click to view]
Re: #temporary table in EXEC Rad
8/1/2006 12:19:08 AM
[quoted text, click to view]
I guess I missed the fact, that I *have* to create that temp table
implicitly anyway ...
Re: #temporary table in EXEC Rad
8/1/2006 12:26:58 AM
[quoted text, click to view]
Yeah, this way - as Omnibuzz wrote its a dead end.

Thanks to all.
Re: #temporary table in EXEC Chris Lim
8/1/2006 12:56:34 AM
[quoted text, click to view]

This is true, however if you create the temporary table BEFORE doing
the EXEC() and do an INSERT..SELECT instead of SELECT INTO, then the
calling procedure will be able to see what the dynamic SQL returned.

e.g.

CREATE TABLE #temp1(test INT NOT NULL)

EXEC('INSERT #temp1 SELECT 1')

SELECT * FROM #temp1
Re: #temporary table in EXEC Omnibuzz
8/1/2006 1:37:01 AM
[quoted text, click to view]

Its more like the dynamic SQL is able to see what the calling procedure
created :)

because the scope of the calling procedure still remains (along with the
temp table) when the EXEC is called and ended. And since the EXEC started
within the scope of the calling procedure, the child process is able to use
it...

Excerpt from BOL that answers you question:

1.Temporary tables are automatically dropped when they go out of scope,
unless explicitly dropped using DROP TABLE:

2.A local temporary table created in a stored procedure is dropped
automatically when the stored procedure completes. The table can be
referenced by any nested stored procedures executed by the stored procedure
that created the table.

3.The table cannot be referenced by the process which called the stored
procedure that created the table.

:)
--
-Omnibuzz (The SQL GC)

http://omnibuzz-sql.blogspot.com/

Re: #temporary table in EXEC Omnibuzz
8/1/2006 1:45:02 AM
I am just wondering if something like this is fool proof.. I can understand
its not an option or a solution to the question...


declare @a uniqueidentifier
set @a = newid()
exec('select top 10 * into [##temp' + @a + '] from sysobjects')

exec('select * from [##temp' + @a +']')

Let me know ur thoughts..
--
-Omnibuzz (The SQL GC)

http://omnibuzz-sql.blogspot.com/

Re: #temporary table in EXEC Omnibuzz
8/1/2006 3:39:02 AM
Yes.. I meant for the unique name and as you say spid should be sufficient :)
--
-Omnibuzz (The SQL GC)

http://omnibuzz-sql.blogspot.com/



[quoted text, click to view]
Re: #temporary table in EXEC Mike C#
8/1/2006 1:22:31 PM
Just a simple question, but what do you do with a table that you don't know
the structure of in advance exactly?

[quoted text, click to view]

Re: #temporary table in EXEC Rad
8/1/2006 9:45:37 PM
[quoted text, click to view]
It doesn't matter already, I went the wrong direction... :)

Anyway, the idea is that client application works with data in that
temporary table and then calls stored procedure to save data into
persistent tables and all this stuff uses column names and primary keys
to identify neede portions of data. Uff..
Re: #temporary table in EXEC Mike C#
8/2/2006 11:41:42 PM

[quoted text, click to view]

Yeah, using a temporary table with an unknown structure would be a
performance killer, and would require you to duplicate a lot of
functionality already built into SQL Server to manage the table. I'm glad
you found a better solution.

RE: #temporary table in EXEC Ravi
8/4/2006 12:43:02 PM
Try This

DECLARE @cQuery nvarchar(1000)


IF (@nStyle = 0)
SET @cQuery = 'SELECT SUSER_SNAME()'
ELSE
SET @cQuery = 'SELECT SUSER_SID()'

SET @cQuery = 'INSERT INTO #t '+@cQuery

IF (OBJECT_ID('tempdb..#t') IS NOT NULL) OR (OBJECT_ID('#t') IS NOT
NULL)
DROP TABLE #t
CREATE TABLE #t(Column1 nvarchar(255))
EXEC(@cQuery)
SELECT * FROM #t


--
Thanks & Rate the Postings.
-Ravi-


[quoted text, click to view]
AddThis Social Bookmark Button