Solved

Using one statement or transaction to insert from one query into 4 tables

Posted on 2012-04-06
4
258 Views
Last Modified: 2012-04-10
Hey experts

The problem I’m trying to solve is that I wrote a query and would like
to Insert the results of this query into 4 tables

select
COLUMN1 as VARIABLE1, 
COLUMN2+'-'+COLUMN3+'-'+COLUMN4 as VARIABLE2,
LEFT(COLUMN2, 2) as VARIABLE3,
RIGHT (COLUMN2, 4) as VARIABLE4,
COLUMN2 as VARIABLE5,
COLUMN3 as VARIABLE6,

case   when COLUMN2 like '%1%' then 'CASE1' 
	 when COLUMN2 like '%2%' then 'CASE2'
	 when COLUMN2 like '%3%' then 'CASE3'
	 end as VARIABLE7,

from TABLE1.COLUMN1 where TABLE1.COLUMN2 like 'MATCH' 
and TABLE2.COLUMN3 = TABLE2.COLUMN4

Open in new window


Prior to inserting I’d like To Check if the data exists in the target tables

Check if exists if not then insert rows for Variables 1,2,3,7 into table 5
Check if exists if not then insert rows for Variables 1,2 into table 6
Check if exists if not then insert rows for Variables 2,7,4 into table 7
Check if exists if not then insert rows for Variables 3,7 into table 8

And finally generate a report of the data inserted.

I learned that the if not exists insert can only run on one table whereas I’m trying to obtain a solution capable of running an insert on multiple tables, so I’m guessing a transaction would be needed? I was able to find a similar thread in SO, but I don’t know how to make it work...
http://stackoverflow.com/questions/175066/sql-server-is-it-possible-to-insert-into-two-tables-at-the-same-time
 
$qry = "INSERT INTO table (one, two, three) VALUES('$one','$two','$three')";
$result = @mysql_query($qry);

$qry2 = "INSERT INTO table2 (two) VALUES('$two')";
$result = @mysql_query($qry2);

Open in new window

0
Comment
Question by:Daniel993
  • 2
  • 2
4 Comments
 
LVL 68

Expert Comment

by:Qlemo
ID: 37816523
Not clear what you are after.
First, you need to define what makes a row we can search for in each of the target tables, i.e. the "keys" for each table.
Then you can only insert into a single table per statement - if it is a requirement that the  rows do not exist in ANY table, you need to check that first, and then execute inserts.
If you, however, just want to make sure not to create duplicate rows, and just need to check that table by table, the INSERT ... SELECT ... if not exists (select ...) is the correct approach.
0
 

Author Comment

by:Daniel993
ID: 37816612
Hey, thanks for a quick reply...

Indeed I'd like to avoid creating duplicate rows table by table (hence the INSERT...Select approach), as for the keys, in tables 5 and 6 the key is variable 1 and in tables 7 and 8 the key is variable 7
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 37817660
One way is to use a temporary table to hold the values, another to keep values in local variables.
select
COLUMN1 as VARIABLE1, 
COLUMN2+'-'+COLUMN3+'-'+COLUMN4 as VARIABLE2,
LEFT(COLUMN2, 2) as VARIABLE3,
RIGHT (COLUMN2, 4) as VARIABLE4,
COLUMN2 as VARIABLE5,
COLUMN3 as VARIABLE6,

case   when COLUMN2 like '%1%' then 'CASE1' 
	 when COLUMN2 like '%2%' then 'CASE2'
	 when COLUMN2 like '%3%' then 'CASE3'
	 end as VARIABLE7

into #vals
from TABLE1.COLUMN1 where TABLE1.COLUMN2 like 'MATCH' 
and TABLE2.COLUMN3 = TABLE2.COLUMN4

insert into table5 (col1, col2, col3, col4)
select variable1, variable2, variable3, variable7 from #vals
where not exists (select * from table5 where col1 = #vals.variable1)

insert into table6 (col1, col2)
select variable1, variable2 from #vals
where not exists (select * from table6 where col1 = #vals.variable1)

insert into table7 (col1, col2, col3)
select variable2, variable7, variable4 from #vals
where not exists (select * from table7 where col2 = #vals.variable7)

insert into table8 (col1, col2)
select variable3, variable7 from #vals
where not exists (select * from table8 where col2 = #vals.variable7)

Open in new window

0
 

Author Closing Comment

by:Daniel993
ID: 37828737
Thanks, it works brilliantly
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

895 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now