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

Temp Tables Expand / Collapse
Author
Message
Posted Tuesday, May 28, 2013 2:48 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, December 12, 2014 10:29 AM
Points: 116, Visits: 258
Danny Ocean (5/24/2013)
I really like this question. It's a very import basic question.
Thanks for question.


+1


MCTS | MCITP | Microsoft SQL Server 2008 Administration & Development
MCSA | MCSE | Business Intelligence SQL Server 2012
Post #1457229
Posted Tuesday, May 28, 2013 5:36 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: Monday, December 1, 2014 4:08 AM
Points: 987, Visits: 567
Christian Buettner-167247 (5/27/2013)
Good question, but the answer is wrong.

SQL Server evaluates object references during parse time. Flow-control logic is not applied at this stage.

This is not true. If you change the temporary table to a regular table (e.g. dbo.Tab), the script will work.
DECLARE @MoreColumns bit;
SET @MoreColumns = 1

IF @MoreColumns = 0
CREATE TABLE dbo.Tab (
id int,
name varchar(50)
);
ELSE
BEGIN
CREATE TABLE dbo.Tab (
id int,
name varchar(50),
Description varchar(8000),
[USER] nvarchar(200)
)
END
GO
DROP TABLE dbo.Tab

If more than one temporary table is created inside a single stored procedure or batch, they must have different names
ms-help://MS.SQLCC.v10/MS.SQLSVR.v10.en/s10de_6tsql/html/1e068443-b9ea-486a-804f-ce7b6e048e8b.htm
Obviously SQL Server does not care if the two create table statements are mutually exclusive and does not allow that.


+1

I think table variable and temp tables will be created at compilation time, so control flow logic will not work here.

can someone pour more light on it?


--
Dineshbabu
Desire to learn new things..
Post #1457269
Posted Tuesday, May 28, 2013 9:02 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 3:03 PM
Points: 1,512, Visits: 554
Interestingly, while the answer does hold for 2005+, for us dinosaurs who still have a few SQL Server 2000 systems, it throws a parse time syntax error on the USER keyword first. Renaming to a different column does still yield the duplicate table name error at parse time, but surprisingly it seems to do the syntax check before catching the "duplicate" table name on my legacy system.

I guess it just goes to show you should always specify version numbers.
Post #1457375
Posted Wednesday, May 29, 2013 8:19 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: Wednesday, August 20, 2014 1:39 PM
Points: 922, Visits: 197
NBSteve (5/28/2013)
Interestingly, while the answer does hold for 2005+, for us dinosaurs who still have a few SQL Server 2000 systems, it throws a parse time syntax error on the USER keyword first. Renaming to a different column does still yield the duplicate table name error at parse time, but surprisingly it seems to do the syntax check before catching the "duplicate" table name on my legacy system.

I guess it just goes to show you should always specify version numbers.


I agree. I tried it first in SQL Server 2000 and received "Incorrect syntax near the keyword 'USER'". Then I realized that it must apply to newer versions, and tested the code in 2008 where it gave the duplicate table error. I think it is a very good question, because it shows how significantly versions differ from each other. Thanks!
Post #1457747
Posted Monday, June 3, 2013 1:41 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: Thursday, November 20, 2014 4:29 AM
Points: 3,559, Visits: 2,671
Good question. I was able to answer as few days back, I think, someone posted the QOTD on same concept. Revision is good.
Post #1459113
Posted Tuesday, June 18, 2013 8:18 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, July 23, 2014 3:24 AM
Points: 1,144, Visits: 299
occured to me few weeks ago

thank's for the question
Post #1464682
« Prev Topic | Next Topic »

Add to briefcase «««123

Permissions Expand / Collapse