Link to home
Start Free TrialLog in
Avatar of Allan
AllanFlag for United States of America

asked on

Error: There is already an object named '#SomeTempTable' in the database

Hi Experts!

So, we've CI / CD for our database development.

Below is a huge script merged from two seperate scripts.
Lines 1-18 is script 1 and 20-36 is script 2.

Weird thing is that at line 27 it would error out:

Msg 2714, Level 16, State 1, Line 27
There is already an object named '#SomeTempTable' in the database.

Any ideas?

    IF OBJECT_ID('Tempdb..#SomeTempTable') IS NOT NULL 
    begin
    select 'IN :' + convert(varchar(10), getdate(), 101)
    DROP TABLE #SomeTempTable
    end
    else      select 'Out :'

    CREATE TABLE #SomeTempTable (
        [RecId] NUMERIC(18, 0) IDENTITY(1,1)
    ,   [Action] VARCHAR(10)
    )

    INSERT INTO #SomeTempTable ([Action])
    VALUES ('INSERT')

--select * from #SomeTempTable
/*Script 1 does something */


    IF OBJECT_ID('Tempdb..#SomeTempTable') IS NOT NULL 
    begin
    select 'IN :' + convert(varchar(10), getdate(), 101)
    DROP TABLE #SomeTempTable
    end
    else select 'Out :'

    CREATE TABLE #SomeTempTable (
        [RecId] NUMERIC(18, 0) IDENTITY(1,1)
    ,   [Action] VARCHAR(10)
    )

    INSERT INTO #SomeTempTable ([Action])
    VALUES ('UPDATE')

--select * from #SomeTempTable
/*Script 2 does something  else*/

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Pavel Celba
Pavel Celba
Flag of Czechia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Allan

ASKER

Thanks for link. We ended up putting a GO after each script.
Different dev working on different scripts,so it's difficult to manage temp names.