AnishaJain AnishaJain - 5 months ago 12
SQL Question

There is already an object named '#xxx' in the database despite drop statement

I have a stored procedure where there are two

INSERT INTO
statements in an
if
else
block, one happening in
if
and the other in
else
so they don't clash. But while compiling, from the one in the else part it gives there Is already an object named '#xxx' despite it being preceded by a drop statement.

IF OBJECT_ID('tempdb..#xxx') IS NOT NULL
DROP TABLE #xxx

SELECT *
INTO #xxx
FROM FamilyDetails_tbl FD(NOLOCK)


Can anyone help?

Answer

Put your select statement into an exec command like this:

exec('SELECT *
    INTO #xxx
    FROM FamilyDetails_tbl FD(NOLOCK)')
Comments