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

LVL 5
allanau20Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pcelbaCommented:
You have to split the code into two batches - simply add  GO  command before the second CREATE command or even before the  second IF command.

Look at similar problems: https://www.mssqltips.com/sqlservertip/1763/sql-server-work-around-for-msg-2714-there-is-already-an-object-named-temp-in-the-database/

    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 */

GO

    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

Optionally you may use TRUNCATE TABLE instead of the 2nd DROP/CREATE
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ryan ChongCommented:
i would simply use different temp table names for different processes, like:

CREATE TABLE #SomeTempTable
..
CREATE TABLE #SomeTempTableElse

Open in new window


benefit:
-easier for understanding and debugging
0
allanau20Author Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.