Solved

Store Procedure....

Posted on 1998-08-19
4
201 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

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

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

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

749 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