Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Inserting...help!

Author  Topic 

brendalisalowe
Constraint Violating Yak Guru

269 Posts

Posted - 2004-08-13 : 13:24:54
I want to be able to insert records from one table to another, but if if already exists I don't want to insert it. I have this, but it doesn't work:

IF NOT EXISTS(SELECT * INTO tblForeverHUD FROM tblHUDdata)

What is the proper way to do it? Thanks!

Brenda

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2004-08-13 : 13:28:20
will this suffice?

if not exists(select ... from MyTable where some condition)
begin
insert into MyTable select ....
end
else
begin
-- something else
end


Go with the flow & have fun! Else fight the flow :)
Go to Top of Page

brendalisalowe
Constraint Violating Yak Guru

269 Posts

Posted - 2004-08-13 : 13:38:18
But what do I put in the WHERE condition? I want everything to be inserted it it is not already in the table. Thanks!

Brenda
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2004-08-13 : 13:44:32
this will select the rows from SomeTable that are not in MyTable and insert them into MyTable

insert into MyTable(col1, col2, ...)
SELECT col1, col2, ...
FROM SomeTable t1
WHERE NOT EXISTS
(SELECT * FROM MyTable t2
WHERE t2.id = t1.id)

will that be ok? :)

Go with the flow & have fun! Else fight the flow :)
Go to Top of Page

brendalisalowe
Constraint Violating Yak Guru

269 Posts

Posted - 2004-08-13 : 14:10:43
That works perfect! Thanks bunches
Go to Top of Page
   

- Advertisement -