Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Temp Tables


Temp Tables

Author
Message
ITGuy268
ITGuy268
SSC-Enthusiastic
SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)

Group: General Forum Members
Points: 126 Visits: 277
Danny Ocean (5/24/2013)
I really like this question. It's a very import basic question.
Thanks for question. :-)


+1 :-D

MCTS | MCITP | Microsoft SQL Server 2008 Administration & Development
MCSA | MCSE | Business Intelligence SQL Server 2012
Dineshbabu
Dineshbabu
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1076 Visits: 569
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..
NBSteve
NBSteve
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2126 Visits: 891
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.
Enigma475
Enigma475
SSC Eights!
SSC Eights! (929 reputation)SSC Eights! (929 reputation)SSC Eights! (929 reputation)SSC Eights! (929 reputation)SSC Eights! (929 reputation)SSC Eights! (929 reputation)SSC Eights! (929 reputation)SSC Eights! (929 reputation)

Group: General Forum Members
Points: 929 Visits: 200
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!
sqlnaive
sqlnaive
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3683 Visits: 2774
Good question. I was able to answer as few days back, I think, someone posted the QOTD on same concept. Revision is good.
jfgoude
jfgoude
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1170 Visits: 299
occured to me few weeks ago

thank's for the question
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search