Temp Tables

  • Very Good One.

    Vinu Vijayan

    For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden[/url] ๐Ÿ˜‰

  • i faced that issue so i knew the ans

    Neeraj prasad sharma

    [/url]

    Neeraj Prasad Sharma
    Sql Server Tutorials

  • 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),

    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.

    Best Regards,

    Chris Bรผttner

  • 1 +

    Neeraj Prasad Sharma
    Sql Server Tutorials

  • 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.[/quote]

    +1 nice question

    Manik
    You cannot get to the top by sitting on your bottom.

  • 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

  • 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),

    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..

  • 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.

  • 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!

  • Good question. I was able to answer as few days back, I think, someone posted the QOTD on same concept. Revision is good.

  • occured to me few weeks ago

    thank's for the question

Viewing 11 posts - 16 through 25 (of 25 total)

You must be logged in to reply to this topic. Login to reply