Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2012-04-06
4
Medium Priority
?
269 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
[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
  • 2
  • 2
4 Comments
 
LVL 71

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 71

Accepted Solution

by:
Qlemo earned 2000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

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
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

618 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