Solved

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

Posted on 2014-01-14
3
254 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
In or Between 2 44
Distributed Replay - When should i use it? 1 25
AWE-based memory on 32-bit servers 1 23
Generate Weekly Schedule 15 17
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

16 Experts available now in Live!

Get 1:1 Help Now