Solved

SQL insert value into table if does not already exist

Posted on 2010-08-28
6
1,088 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

In this article I will describe the Backup & Restore 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.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

808 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