Solved

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

Posted on 2014-01-14
3
260 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
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 brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

636 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