Solved

SQL insert value into table if does not already exist

Posted on 2010-08-28
6
1,053 Views
Last Modified: 2012-05-10
For my database with two tables: Table1, Table2
There is a shared field:Column1

I need to create a SQL statement that checks every Column1 value in Table1.  If that value does not already exist in Column1 of Table2, then insert the value into Column1 of Table2.

Any assistance is appreciated.
0
Comment
Question by:Jorhal
6 Comments
 

Expert Comment

by:Ataraxia2010
ID: 33548812
Try This:
Select @count=count(*) from table where condition......

if( @count>0)
Begin
Print 'match'
End

Else
Begin
print 'no match'
End
0
 
LVL 6

Expert Comment

by:radnbne
ID: 33548813
You simply right click on one of the application bars in the navigation pane and select open in new window, then drag the window to your other screen.

0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 33548815
INSERT INTO Table2
SELECT * FROM Table1 WHERE Column1 NOT IN (SELECT Column1 FROM Table2)

I understood like - You need to insert those records into Table2, that present in Table1, but not in Table2
Raj


0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 23

Accepted Solution

by:
Rajkumar Gs earned 500 total points
ID: 33548820
If there is any primary key with identity property, specify other columns excluding that identity column.
Like

INSERT INTO Table2 (name, address, Column1)
SELECT name, address, Column1 FROM Table1
WHERE Column1 NOT IN (SELECT Column1 FROM Table2)

Raj
0
 
LVL 7

Expert Comment

by:rmm2001
ID: 33560058
INSERT INTO Table2 (Column1)
    SELECT t1.Column1
    FROM Table1 t1
    LEFT JOIN Table2 t2 ON t1.Column1 = t2.Column1
    WHERE t2.Column1 IS NULL

or if the key value in table 2 exists with a null field in column1 and you're trying to make 2 tables that look alike

    UPDATE Table2
    SET Column1 = t1.Column1
    FROM Table1 t1
    JOIN Table2 t2 ON t1.KeyValue = t2.KeyValue
    WHERE t2.Column1 IS NULL
0
 
LVL 4

Expert Comment

by:parthmalhan
ID: 33560454
Please make sure that the 2 tables have the exactly same fields in the same order

try the following code:

insert into table2
select * from table1
inner join table 2 on table1.column1<>table2.column1
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

758 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

21 Experts available now in Live!

Get 1:1 Help Now