Solved

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

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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PL/SQL query 14 51
Classic ASP application Will support SQL 2014 5 36
Query 14 22
DATETIMEOFFSET feature 1 5
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

744 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

10 Experts available now in Live!

Get 1:1 Help Now