Solved

How Do You Update a Table With Rows from Another If Rows Do Not Exist?

Posted on 2014-01-14
3
253 Views
Last Modified: 2014-01-14
I need to write an SQL Stored Procedure to insert values into one table if those values do not exist in the other table.  How can this be done using MS SQL Server SQL?

Thanks,

Dan
0
Comment
Question by:danielolorenz
3 Comments
 
LVL 25

Expert Comment

by:Shaun Kline
Comment Utility
If you are talking about inserting a single row of data, you can use:
IF NOT EXISTS (select 1 from <table to check> where <pk of table> = <value to insert>
   insert ...

If you have data in one table that you need to insert into a second table:
insert into <target table>
(<column list>)
select <columns to insert>
from <source table>
where NOT EXISTS (select 1 from <target table> where <target table PK> = <source table>.<source table PK>)
0
 
LVL 3

Accepted Solution

by:
13Shadow earned 250 total points
Comment Utility
Try something like this:
CREATE PROCEDURE dbo.proc_Insert
    @ID INT, @Name VARCHAR(50), @Field1 INT
AS
    MERGE dbo.YourTable AS t
    USING (SELECT @ID, @Name, @Field1 ) AS Source(ID, NAME, Field1 )
    ON source.ID = t.ID AND source.Name = t.Name

      WHEN NOT MATCHED THEN
        INSERT(ID, Name, Field1 )
        VALUES(source.ID, source.Name, source.Field1 );
0
 
LVL 11

Assisted Solution

by:John_Vidmar
John_Vidmar earned 250 total points
Comment Utility
insert	missing_records_table
(	field1
,	field2
,	...
,	fieldN
)
select	field1
,	field2
,	...
,	fieldN
from	all_records_table	a
left
join	some_missing_table	b	on	a.primary_key = b.primary_key
where	b.primary_key is null

Open in new window

0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

763 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

7 Experts available now in Live!

Get 1:1 Help Now