Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

control of flow issue

$
0
0

Hi - Below is some sample code to help demonstrate this dilemma...

declare
@Type1Orders       INT,        --1 for yes, 0 for no
@Type2Orders     INT;        --1 for yes, 0 for no

set @Type1Orders = 0;
set @Type2Orders = 1;

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

if @Type1Orders = 1
begin
  create table ##temp1 (col1 int, col2 int);
  insert into ##temp1 (col1, col2) values
  (1,1),
  (2,2);
end

if @Type2Orders = 1
begin
  create table ##temp1 (col3 int, col4 int);
  insert into ##temp1 (col3, col4) values
  (3,3),
  (4,4);
end
go
When this code is executed the error message "There is already an object named '##temp1' in the database." occurs.  Why is it doing this?  How can it be fixed?  I have a strong preference to keep to the DROP TABLE statement out of the IF blocks so that ##temp1 is always dropped if it exists.



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>