473,756 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

insert into temp table based on if condition

das
hello all,
this might be simple:

I populate a temp table based on a condition from another table:

select @condition = condition from table1 where id=1 [this will give
me either 0 or 1]

in my stored procedure I want to do this:

if @condition = 0
begin
select * into #tmp_table
from products p
inner join
sales s on p.p_data = s.p_data
end
else
begin
select * into #tmp_table
from products p
left join
sales s on p.p_data = s.p_data
end

Tha above query would not work since SQL thinks I am trying to use the
same temp table twice.

As you can see the major thing that gets effected with the condiction
being 0/1 is the join (inner or outer). The actual SQL is much bigger
with other joins but the only thing changing in the 2 sql's is the join
between products and sales tables.

any ideas gurus on how to use different sql's into temp table based on
the condition?

thanks
adi

Apr 12 '06 #1
5 15038
This guy had the same problem:
http://www.experts-exchange.com/Data..._21594067.html

Doesn't look like they found much of a solution :(

Maybe you could try something with table variables and dynamic SQL?

Apr 12 '06 #2
select * into #tmp_table
from products p
left join
sales s on p.p_data = s.p_data
where s.p_data IS NOT NULL OR
@condition <> 0
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Apr 12 '06 #3
You have to create your temp table outside of your select statements
and then insert into it. For example,

CREATE TABLE #tmp_table
AS SELECT *
FROM PRODUCTS
WHERE 1 = 2

IF @condition = 0
BEGIN
INSERT #tmp_table
SELECT *
FROM products ....
END
ELSE
....

Hope it helps
Teresa

das wrote:
hello all,
this might be simple:

I populate a temp table based on a condition from another table:

select @condition = condition from table1 where id=1 [this will give
me either 0 or 1]

in my stored procedure I want to do this:

if @condition = 0
begin
select * into #tmp_table
from products p
inner join
sales s on p.p_data = s.p_data
end
else
begin
select * into #tmp_table
from products p
left join
sales s on p.p_data = s.p_data
end

Tha above query would not work since SQL thinks I am trying to use the
same temp table twice.

As you can see the major thing that gets effected with the condiction
being 0/1 is the join (inner or outer). The actual SQL is much bigger
with other joins but the only thing changing in the 2 sql's is the join
between products and sales tables.

any ideas gurus on how to use different sql's into temp table based on
the condition?

thanks
adi


Apr 12 '06 #4
Genius.

Apr 12 '06 #5
das
Thats a cool idea, I almost am half-bald over this problem.
but for me your solution works.

Just curious, how can we create a empty table with this statement?

CREATE TABLE #tmp_table
AS SELECT *
FROM PRODUCTS
WHERE 1 = 2

I had to use:

SLECT * INTO #tmp_table
FROM PRODUCTS
WHERE 1 = 2

and then I used the if condition.

thanks a lot again!

Apr 12 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
10238
by: Alex Vorobiev | last post by:
hi there, i am using sql server 7. below is the stored procedure that is giving me grief. its purpose it two-fold, depending on how it is called: either to return a pageset (based on page number and page size), or to return IDs of previous and next records (based on current record id). the problem is, that the order in which records are inserted into the temp table is inconsistent, even though the calling statement and the order by is...
4
5485
by: Chris Kratz | last post by:
Hello all, We have run into what appears to be a problem with rules and subselects in postgres 7.4.1. We have boiled it down to the following test case. If anyone has any thoughts as to why this would be happening, we would appreciate feedback. We have tested on 7.3.4, 7.3.6 and 7.4.1 and all exhibit the same behavior. Test case one tries to populate table2 from table1 with records that are not in table2 already. Table2 gets...
2
4623
by: Timothy Perrigo | last post by:
I'm working on a plpgsql function that creates and populates a temporary table. I would like the function to first drop the temp table, if it already exists. I'm not sure how to accomplish this, though. My first inclination was to simply wrap the 'drop table' command in an exception handling block and ignore the exception if the table does not exist. I'm not sure what error condition to catch, though, so rather than specifying a "WHEN"...
3
1827
by: rhaazy | last post by:
Using ms sql 2000 I have 2 tables. I have a table which has information regarding a computer scan. Each record in this table has a column called MAC which is the unique ID for each Scan. The table in question holds the various scan results of every scan from different computers. I have an insert statement that works however I am having troulbe getting and update statement out of it, not sure if I'm using the correct method to insert...
9
2275
by: rhaazy | last post by:
Using MS SQL 2000 I have a stored procedure that processes an XML file generated from an Audit program. The XML looks somewhat like this: <ComputerScan> <scanheader> <ScanDate>somedate&time</ScanDate>
8
3520
by: nano2k | last post by:
Hi Shortly, I keep invoices in a table. Occasionally, someone will fire the execution of a stored procedure (SP) that performs several UPDATEs against (potentially) all invoices OLDER than a date that is supplied to the SP as a parameter. The SP is usually a lengthy process (it takes at least 30 mins). The problem is that SQL server 2000 Dev Edition doesn't allow me to insert new invoices that are "younger", while the SP is executing....
21
4693
by: lesperancer | last post by:
I've got an access97 reporting mdb that pulls data (77,000 rows) from a sql server table into a local table to run reports if the local table is part of the reporting MDB, the insert statement (16 fields) takes less than 30secs, but because of db-bloat, I moved the local table to a 2nd MDB and per postings, this 2nd MDB is copied into a folder and linked as a 'temp' MDB every time I run my reporting mdb
1
8724
by: albertleng | last post by:
Hi all... Can anyone give clue in doing below? Let say i have a table A with the following 3 fields. 1)Date 2)Title 3)Status. i need to insert into table B (also have the same 3 fields) from this table A with the condition where Title is "Alarm" and Status is "ON". This can be done by a simple "INSERT" query.
6
3720
by: lenygold via DBMonster.com | last post by:
Hi everybody: What is the best way to I have 10 tables with similar INSERT requiremnts. INSERT INTO ACSB.VAATAFAE WITH AA(AA_TIN, AA_FILE_SOURCE_CD, .AA_TIN_TYP) AS ( SELECT AA_TIN, AA_FILE_SOURCE_CD, .AA_TIN_TYP FROM VAATAFAA WHERE AB_TP_ACNT_STAT_CD <0),
0
9431
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10014
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9844
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9689
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8688
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7226
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5289
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3326
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2647
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.