Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-01-14
3
Medium Priority
?
263 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 27

Expert Comment

by:Shaun Kline
ID: 39779500
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 1000 total points
ID: 39779510
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 1000 total points
ID: 39779962
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

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…

876 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