create table from if statement SQL

481 Views Asked by At

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.

0

There are 0 best solutions below