?
Solved

Creating temporary table inside a conditional clause

Posted on 2003-02-27
4
Medium Priority
?
315 Views
Last Modified: 2008-03-04
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.
0
Comment
Question by:RobinXavier
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 1

Accepted Solution

by:
tenderfoot earned 200 total points
ID: 8056975
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
 

Author Comment

by:RobinXavier
ID: 8092955
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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
If you're a modern-day technology professional, you may be wondering if certifications are really necessary. They are. Here's why.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question