Solved

Store Procedure....

Posted on 1998-08-19
4
200 Views
Last Modified: 2010-03-19
How do I create a stored procedure to copy the contents
of one table (with a where statement on a field) to another
table that already exist and MAY have data in it already that we can't kill?
0
Comment
Question by:ohayden
4 Comments
 
LVL 4

Expert Comment

by:mcix
ID: 1089640
Do the tables have the same format?
0
 
LVL 4

Expert Comment

by:mitek
ID: 1089641
Something like that:

INSERT INTO tab1
  SELECT c1,c2,c3 FROM tab2 -- select only those columns that will match the columns of the first table
   WHERE c1 = 'NY' -- any condition

If that's notwhat you meant, please rephrase your question
(as someone noticed, a well asked question already contains 1/2 of answer :)

0
 
LVL 3

Accepted Solution

by:
gmoriak earned 50 total points
ID: 1089642
Here's all scenarios I can think of

/* insert all rows no matter what */
Insert A (pkCol, col2, col3)
(select B.pkCol, B.col2, B.col3
 from B)

/* insert on rows in B not existing in A */
Insert A (pkCol, col2, col3)
(select B.pkCol, B.col2, B.col3
 from B
 where not exists (select A2.pkCol from A2 where A2.pkCol = B.pkCol)


0
 

Author Comment

by:ohayden
ID: 1089643
I already solved the problem but your answer was exactly how I did it, so even though I did use your answer the points are yours..... thanks.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

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…
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.
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.

856 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