Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

730 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