Solved

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

Posted on 2012-04-06
4
261 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 69

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 69

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

778 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