Background: There's a stored procedure which does "stuff" with a temp table of a given name. The procedure is generic in that it inspects the schema of the temp table and then does different "stuff" depending on the schema. I understand that this is a little weird but I'm reluctant to change it because it all works fine in most situations, except....
If I have a stored procedure which creates two different schemas for a temp table with the same name. Logically it only creates one temp table depending on which branch of the IF. The problem is that when the Sproc is checked by SQL Server it seems like it is evaluating both sides of the IF (which makes sense if it's checking the SQL syntax.)
So this SQL fails:
IF (1=1)
BEGIN
CREATE TABLE #test
(
a BIGINT NOT NULL,
b BIGINT NOT NULL
)
END
ELSE
BEGIN
CREATE TABLE #test
(
a BIGINT NOT NULL,
b BIGINT NOT NULL,
c BIGINT NOT NULL
)
END
--exec SomeProcedureWhichDoesStuffWith#Test
DROP TABLE #test
with the following error:
Msg 2714, Level 16, State 1, Line 14
There is already an object named
'#test' in the database.
No combination of drop table inside the ifs (before or after the create table DDL) seems to satisfy the sql checker.
Any ideas how I can do this? Can I, for example, tell SQL to not perform the syntax check and just accept the sproc as is?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…