Solved

SQL insert value into table if does not already exist

Posted on 2010-08-28
6
1,078 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

832 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