0

I have this code in my SQL, however the if statement seems to be not working at all. Every time I run the query below.. I always have an error that says...

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

QUERY BELOW...

IF (SELECT COUNT (*) FROM ##TEMP1 WHERE [ACTION] NOT LIKE 'ok' ) >=1
        BEGIN               
                SELECT * INTO ##newtable FROM ##TEMP1 ORDER BY 1 ASC

                Select ' ' = 'MESSAGE' 
                SELECT * FROM ##newtable
        END
    ELSE
        BEGIN
                Select [MESSAGE] = 'MESSAGE' INTO ##newtable
                SELECT * FROM ##newtable
        END

is there any way that the query creates a final table '##temp1' given that the condition is met? So it's either the first begin statement or the second one.

Thank you.

3
  • You're trying to create the same object twice in the same batch, hence the failure. Commented Apr 8, 2020 at 12:45
  • @Lamu, do you know how I can fix this? I just want a final table give that any condition is met. Commented Apr 8, 2020 at 13:09
  • If you don't use temporary tables, you could use sp_executesql. Commented Apr 8, 2020 at 14:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.