Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Creating temporary table inside a conditional clause

Posted on 2003-02-27
4
Medium Priority
?
318 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
2 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

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

I’m willing to make a bet that your organization stores sensitive data in your Windows File Servers; files and folders that you really don’t want making it into the wrong hands.
The article explains the process to deploy a Self-Service password reset portal I developed a few years ago. Hopefully, it will prove useful to someone.  Any comments, bug reports etc. are welcome...
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

580 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