• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

Store Procedure....

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
ohayden
Asked:
ohayden
1 Solution
 
mcixCommented:
Do the tables have the same format?
0
 
mitekCommented:
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
 
gmoriakCommented:
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
 
ohaydenAuthor Commented:
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

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now