I want to have a construct in a stored procedure that defines a temporary table one way in one branch of an if-else and another way in the other branch, but this won't compile b/c it says the table is already created. I am imagining something like the following:
if
begin
IF OBJECT_ID(N'tempdb..#tbl') IS NOT NULL DROP TABLE #tbl
create table #tbl (A int, B int)
end
else
begin
IF OBJECT_ID(N'tempdb..#tbl') IS NOT NULL DROP TABLE #tbl
create table #tbl (A int, B int, C int)
end
which seems like it should be well defined.
Is there a better way to do this than declaring it empty and then repeatedly altering it to add the columns I want in the branches? (this is just kind of ugly)