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 Wednesday, November 17, 2010 8:05 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 8:26 AM
Points: 2,142, Visits: 1,739
Comments posted to this topic are about the item SELECT INTO with a Temp Table
Post #1022586
Posted Wednesday, November 17, 2010 9:17 PM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Thursday, January 2, 2014 9:57 AM
Points: 554, Visits: 863
hi,

Nice Question . Can u pls provide the reference links to understand more about this.

Thanks
Deepak.A
Post #1022601
Posted Thursday, November 18, 2010 1:43 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: Today @ 12:27 PM
Points: 986, Visits: 954
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

Post #1022669
Posted Thursday, November 18, 2010 1:48 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, September 2, 2014 10:39 AM
Points: 1,194, Visits: 787
Nice Question.Generally we will prefer to create the table first and then try to insert into it in these case.
Post #1022672
Posted Thursday, November 18, 2010 1:50 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: Today @ 12:27 PM
Points: 986, Visits: 954
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
Post #1022673
Posted Thursday, November 18, 2010 1:59 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 12:26 PM
Points: 6,007, Visits: 8,269
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).

deepak.a (11/17/2010)
Can u pls provide the reference links to understand more about this.

I'm not sure if this is documented anywhere (if it is, I've never found it), but the explanation is that SQL Server uses is a one-pass parser and comiler. That implies that it reads the code once, top to bottom and left to right. Variables have to be declared before they are referenced, and exactly once. Similar for table creation and referencing.
One effect of this one-pass parser is that control-flow is not evaluated. So a variable declaration in a loop is not an error, and creating a table in a loop will not cause a compile-time error (the error will be at run-time, during the second execution of the loop). It is also possible to have declarations in code paths that will never execute. For instance, this code compiles and executes just fine:
IF 1 = 2
BEGIN;
DECLARE @a int;
END;
SET @a = 1;
PRINT @a;

And similarly, this code will execute without errors, but fails at run-time.
IF 1 = 2
BEGIN;
CREATE TABLE #xyz (a int)
END;
INSERT INTO #xyz(a) VALUES(1);
SELECT * FROM #xyz;

This rarely ever affects "normal" code - except in cases such as this question, where people try to use SELECT INTO in two different branches of an IF statement.

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:

No, I would not recommend resorting to dynamic SQL. As the explanation of the question suggests, the best course of action is to create the table seperately, then fill it in the IF. So the code in this question should change to either
CREATE PROCEDURE QOTD (@source INT)
AS
BEGIN;
CREATE TABLE #tmpID (ID int);
IF @source = 1
INSERT INTO #tmpID (ID)
SELECT ID FROM tbl1;
ELSE
INSERT INTO #tmpID (ID)
SELECT ID FROM tbl2;

SELECT ID FROM #tmpID;
END;

or
CREATE PROCEDURE QOTD (@source INT)
AS
BEGIN;
SELECT ID INTO #tmpID FROM tbl1 WHERE 1 = 2;
IF @source = 1
INSERT INTO #tmpID (ID)
SELECT ID FROM tbl1;
ELSE
INSERT INTO #tmpID (ID)
SELECT ID FROM tbl2;

SELECT ID FROM #tmpID;
END;




Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #1022675
Posted Thursday, November 18, 2010 2:01 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: Tuesday, September 9, 2014 3:35 PM
Points: 831, Visits: 1,581
@Iulian -207023: Yes totally agree about the GO and drops. Wasn't sure whether the missing GO was some trick part of the question. Very interesting quirk to be aware of.



One of the symptoms of an approaching nervous breakdown is the belief that one's work is terribly important.
Bertrand Russell
Post #1022677
Posted Thursday, November 18, 2010 2:08 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 8:26 AM
Points: 2,142, Visits: 1,739
Thanls for the feedback so far. Apologies, the GO and Drops were an omission but I hope that didn't detract too much from the meaning behind the question. It's the first contribution I've made to the site so I'll do better next time ;)
Like Hugo I only came across this when I tried to do it at work so I thought I'd share the experience.
Post #1022683
Posted Thursday, November 18, 2010 3:00 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: Today @ 12:27 PM
Points: 986, Visits: 954
Thanks a lot Hugo

Iulian
Post #1022715
Posted Thursday, November 18, 2010 3:32 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: 2 days ago @ 3:47 PM
Points: 1,778, Visits: 5,730
Thanks for a good question.

MM


  • MMGrid Addin
  • MMNose Addin


  • Forum Etiquette: How to post Reporting Services problems
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw

  • Post #1022724
    « Prev Topic | Next Topic »

    Add to briefcase 123»»»

    Permissions Expand / Collapse