Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««123»»

SELECT INTO with a Temp Table Expand / Collapse
Author
Message
Posted Thursday, November 18, 2010 5:31 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, March 17, 2014 5:44 AM
Points: 393, Visits: 516
I didn't read beyond the CREATE PROCEDURE. It wasn't the first line of the batch so I assumed "procedure would not run" would be the answer.


Post #1022792
Posted Thursday, November 18, 2010 5:45 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: 2 days ago @ 11:59 PM
Points: 894, Visits: 889
Excellent question. I came up with five or six possible reasons why the SP might not work, and ruled each of them out in turn. Of course I never thought of that one! Glad to have learned something this morning.

Ron Moses


-----
a haiku...

NULL is not zero
NULL is not an empty string
NULL is the unknown
Post #1022799
Posted Thursday, November 18, 2010 6:18 AM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Friday, May 9, 2014 12:47 AM
Points: 3,448, Visits: 4,407
Iulian -207023 (11/18/2010)
Do you have at hand some best practices for using DML statements? I am thinking on something like:
...
SET @strQueryDML = 'SELECT ID INTO #tmpID2 FROM tbl1'
...
EXECUTE (@strQueryDML)
...
SELECT ID FROM #tmpID2


This code fails with the error message "Invalid object name #tmpID2". In this case, the local temprorary table "#tmpID2" exists only within the scope of the dynamic SQL. When the execution of the dynamic SQL is complete, SQL Server deletes the table. Of course, you may move the SELECT statement into the D-SQL piece of code, but it would be (to put it mildly) not the best decision
Post #1022825
Posted Thursday, November 18, 2010 6:24 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, November 20, 2014 8:13 AM
Points: 2,917, Visits: 2,536
Excellent question on a topic which is such a problem. I see this a lot from developers who wonder why it won't work. At first glance, it looks like it shuld and programatically should. But then apply logic

Steve Jimmo
Sr DBA
“If we ever forget that we are One Nation Under God, then we will be a Nation gone under." - Ronald Reagan
Post #1022833
Posted Thursday, November 18, 2010 7:28 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 9:10 AM
Points: 13,230, Visits: 12,709
Great question. Keep them coming!!

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1022891
Posted Thursday, November 18, 2010 7:33 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 8:00 AM
Points: 985, Visits: 1,839
Iulian -207023 (11/18/2010)
One innocent remark that does not minimize the high value of the question. I am thinking to drop tbl1 and tb2 tables at the end of the script:
DROP TABLE tbl1
DROP TABLE tbl2

and to place a GO statement before CREATE PROCEDURE since 'CREATE/ALTER PROCEDURE' must be the first statement in a query batch.

Regards,
Iulian


This is exactly why I got this right -- I assumed that the procedure would not compile because it was not the first statement in a batch the way it was written.
Post #1022895
Posted Thursday, November 18, 2010 7:35 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, September 30, 2014 7:42 AM
Points: 3,688, Visits: 72,435
Iulian -207023 (11/18/2010)
Great point. Amazing, the compiler see two creations of the #tmpID table and stops the procedure from compiling.
Do you have at hand some best practices for using DML statements? I am thinking on something like:
CREATE PROCEDURE QOTD (@source INT)
AS
BEGIN
DECLARE @strQueryDML AS VARCHAR(100)

IF @source = 1
SET @strQueryDML = 'SELECT ID INTO #tmpID2 FROM tbl1'
ELSE
SET @strQueryDML = 'SELECT ID INTO #tmpID2 FROM tbl2'

EXECUTE (@strQueryDML)

SELECT ID FROM #tmpID2

END



The DML won't work that way because the temp table that you create in the DML will be released when the DML finishes executing.

One of two solutions to this.

 IF @source = 1 
SET @strQueryDML = 'SELECT ID INTO #tmpID2 FROM tbl1;
SELECT * FROM #tmpID2'
ELSE
SET @strQueryDML = 'SELECT ID INTO #tmpID2 FROM tbl2;
SELECT * FROM #tmpID2'

Or just create the temp table before the IF/ELSE block and use insert INTO instead.





--Mark Tassin
MCITP - SQL Server DBA
Proud member of the Anti-RBAR alliance.
For help with Performance click this link
For tips on how to post your problems
Post #1022901
Posted Thursday, November 18, 2010 8:31 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, August 6, 2013 4:21 PM
Points: 41, Visits: 71
Got it right, but apparently not for the knowledge that the question was intended to test for.

I assumed it wouldn't compile because the create procedure part wasn't in its own batch.
Post #1022958
Posted Thursday, November 18, 2010 8:43 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, January 31, 2013 8:01 AM
Points: 1,228, Visits: 1,046
I also was one that selected the right answer because thier where so many errors in the code I determined that was either the right answer, or the one most people would select.

Is there an FAQ for people making QOTD submisions?

You would think that as long as QOTD has been going on that we would have one.
I know I could use one.
Post #1022973
Posted Thursday, November 18, 2010 9:02 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Yesterday @ 9:31 AM
Points: 717, Visits: 3,037
Hugo Kornelis (11/18/2010)
Good question; many people would expect this to work. (As I did, the first time I ran into this. And the second, third, ... - I answered correctly but only because I've run into this often enough to cause a permanent dent in my forehead).


I can only echo Hugo's comment about why I immediately knew the answer before seeing the available multiple choice options: miserable first-hand experience.

In my case, though, "third" would be followed by "nth", where n was an embarrassingly large integer.

The not-quite-truthful error-message returned in SSMS, "There is already an object named '#MyNonexistentTempTable' in the database.", had me trying to DROP a non-existent table, with, of course, an unproductive outcome.

My take-home lesson on this is that T-SQL doesn't really like even the few elements of procedural code provided to us (couldn't we please have ELSEIF???) The compiler pretty much ignores the fact that the 2 SELECT..INTO statements are mutually exclusive executions.

Good question!

Rich
Post #1022990
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse