sql - Using IF / ELSE to determine a SELECT INTO statement -
i'm having strange issues using if / else determine 1 or 2 select statements execute. error message i'm getting when running full statement temporary table exists, not occur if run 2 separate executions of 2 separate if statements.
here code in sql server:
if (select businessdaycount calendartbl) <= 1 begin select * #temp1 previousmonthtbl end else begin select * #temp1 currentmonthtbl end
it's "feature" of syntax checking in sql server. cannot "create" #temporary table twice within same batch.
this pattern need.
select * #temp1 previousmonthtbl 1=0; if (select businessdaycount calendartbl) <= 1 begin insert #temp1 select * previousmonthtbl end else begin insert #temp1 select * currentmonthtbl end
if prefer, can express branch (in case) clause:
select * #temp1 previousmonthtbl (select businessdaycount calendartbl) <= 1 union select * currentmonthtbl isnull((select businessdaycount calendartbl),2) > 1
Comments
Post a Comment