Solved

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

Posted on 2014-01-14
3
255 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 26

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 250 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 250 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS SQL - Rotating Values in SQL 9 65
Getting max record but maybe not use Group BY 2 27
Sql Query with datetime 3 24
kill process lock Sql server 9 54
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 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