Creating temporary table inside a conditional clause

I want to have the following logic in a stored procedure.

if (a=0)
begin
     select x,y,z
     into #p
     from #q
end
else
begin
     select x,y,z
     into #p
     from #q
end

But when I compile this procedure It gives the error that the temporary table already exists.

I know that I can have a create statement before the conditional clause. And then use the "insert into" statement for inserting the data into temporary table.

But I do not want to use the "insert into" statement as it generates a lot of Transaction Log as a result is much slower.
RobinXavierAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tenderfootCommented:
If you need to insert into #p from both conditions of your 'if' clause, you could use a nested procedure call to enable 'select.. into..'

if(@a=0)
begin
  execute sp_1
end
else
begin
  execute sp_2
end

sp_1
select x,y,z into #p from #q

sp_2
select x,y,z into #p from #q

I'm not entirely certain what you're trying to achieve here
as the example provided indicates that #p will contain the same values independent of 'a'
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RobinXavierAuthor Commented:
The temporary table created in a procedure will exist only till the procedure where it is created exists.

So in the above mentioned approach the temp table would not be available in the parent procedure in which the calls to these procedures are made.

I want to use the table in some other procedures which will be called from the parent procedure down the line.

So I cannot follow the above mentioned approach.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Sybase Database

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.