Check if a temporary table exists and delete if it exists before creating a temporary table

前端 未结 15 602
忘掉有多难
忘掉有多难 2020-11-29 14:13

I am using the following code to check if the temporary table exists and drop the table if it exists before creating again. It works fine as long as I don\'t change the colu

相关标签:
15条回答
  • 2020-11-29 14:58

    This could be accomplished with a single line of code:

    IF OBJECT_ID('tempdb..#tempTableName') IS NOT NULL DROP TABLE #tempTableName;   
    
    0 讨论(0)
  • 2020-11-29 15:01

    When you change a column in a temp table, you must drop the table before running the query again. (Yes, it is annoying. Just what you have to do.)

    I have always assumed this is because the "invalid column" check is done by parser before the query is run, so it is based on the columns in the table before it is dropped..... and that is what pnbs also said.

    0 讨论(0)
  • 2020-11-29 15:05

    I cannot reproduce the error.

    Perhaps I'm not understanding the problem.

    The following works fine for me in SQL Server 2005, with the extra "foo" column appearing in the second select result:

    IF OBJECT_ID('tempdb..#Results') IS NOT NULL DROP TABLE #Results
    GO
    CREATE TABLE #Results ( Company CHAR(3), StepId TINYINT, FieldId TINYINT )
    GO
    select company, stepid, fieldid from #Results
    GO
    ALTER TABLE #Results ADD foo VARCHAR(50) NULL
    GO
    select company, stepid, fieldid, foo from #Results
    GO
    IF OBJECT_ID('tempdb..#Results') IS NOT NULL DROP TABLE #Results
    GO
    
    0 讨论(0)
  • 2020-11-29 15:05

    pmac72 is using GO to break down the query into batches and using an ALTER.

    You appear to be running the same batch but running it twice after changing it: DROP... CREATE... edit... DROP... CREATE..

    Perhaps post your exact code so we can see what is going on.

    0 讨论(0)
  • 2020-11-29 15:05

    I usually hit this error when I have already created the temp table; the code that checks the SQL statement for errors sees the "old" temp table in place and returns a miscount on the number of columns in later statements, as if the temp table was never dropped.

    After changing the number of columns in a temp table after already creating a version with less columns, drop the table and THEN run your query.

    0 讨论(0)
  • 2020-11-29 15:06

    I think the problem is you need to add GO statement in between to separate the execution into batches. As the second drop script i.e. IF OBJECT_ID('tempdb..#Results') IS NOT NULL DROP TABLE #Results did not drop the temp table being part of single batch. Can you please try the below script.

    IF OBJECT_ID('tempdb..#Results') IS NOT NULL
        DROP TABLE #Results
    
    CREATE TABLE #Results
    (
        Company                CHAR(3),
        StepId                TINYINT,
        FieldId                TINYINT,
    )
    
    GO
    
    select company, stepid, fieldid from #Results
    
    IF OBJECT_ID('tempdb..#Results') IS NOT NULL
    DROP TABLE #Results
    
    CREATE TABLE #Results
    (
        Company                CHAR(3),
        StepId                TINYINT,
        FieldId                TINYINT,
        NewColumn            NVARCHAR(50)
    )
    
    GO
    
    select company, stepid, fieldid, NewColumn from #Results
    
    0 讨论(0)
提交回复
热议问题