[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

SQL insert value into table if does not already exist

Posted on 2010-08-28
6
Medium Priority
?
1,136 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
[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
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
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

 
LVL 23

Accepted Solution

by:
Rajkumar Gs earned 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

649 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