|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Today @ 2:11 AM
Points: 1,607,
Visits: 1,090
|
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Monday, August 13, 2012 10:04 AM
Points: 554,
Visits: 861
|
|
hi,
Nice Question . Can u pls provide the reference links to understand more about this.
Thanks Deepak.A
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Saturday, March 16, 2013 9:53 AM
Points: 847,
Visits: 768
|
|
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
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Monday, September 17, 2012 7:30 AM
Points: 1,038,
Visits: 679
|
|
| Nice Question.Generally we will prefer to create the table first and then try to insert into it in these case.
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Saturday, March 16, 2013 9:53 AM
Points: 847,
Visits: 768
|
|
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
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 2:24 AM
Points: 5,235,
Visits: 7,040
|
|
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
|
|
|
|
|
SSChasing Mays
      
Group: General Forum Members
Last Login: Sunday, May 19, 2013 5:30 AM
Points: 648,
Visits: 1,302
|
|
@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
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Today @ 2:11 AM
Points: 1,607,
Visits: 1,090
|
|
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.
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Saturday, March 16, 2013 9:53 AM
Points: 847,
Visits: 768
|
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 2:26 AM
Points: 1,289,
Visits: 3,859
|
|
Thanks for a good question.
MM
|
|
|
|