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
 Transact-SQL (2000)
 insert into without error if conflict with index

Author  Topic 

fabianus76
Posting Yak Master

191 Posts

Posted - 2006-01-04 : 16:40:19
Hello !

I would like to perform an insert into but I do not want SQL-Server (2005 Express) to bother if there is any conflict with an "unique"-index.
Meaning that if a record might not be registered because of any conflict with an index, I want the query to be performed anyway, just by droping what is not possible to do.

Thank you very much for any help!

Regards,
Fabian

my favorit hoster is ASPnix : www.aspnix.com !

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-01-04 : 16:43:03
I am not following what you want. Do you only want to perform an insert if it doesn't exist already? Or do you want to perform and insert and don't care about the error if it does exist already? Please provide an example as some of the wording in your post is a bit confusing.

Tara Kizer
aka tduggan
Go to Top of Page

fabianus76
Posting Yak Master

191 Posts

Posted - 2006-01-04 : 16:51:42
quote:
Originally posted by tkizer

I am not following what you want. Do you only want to perform an insert if it doesn't exist already? Or do you want to perform and insert and don't care about the error if it does exist already? Please provide an example as some of the wording in your post is a bit confusing.

Tara Kizer
aka tduggan



Hallo Tara,
thank you for the quick response!
What I want to do is the follwing:
Imagine you have a table with country names and this table has a unique index on the column of the country names. Now you want to add some new country names into your table. But your INSERT INTO will try to add some country names that are already existing in the table. Now with a normal INSERT INTO, the query won't be executed, but an error will be thrown back telling that there are some conflicts with the unique index on country names.
So what I would like to do is to know if there is any T-SQL writing to add only the country names which are not yet existing in the table.

Thank you for some further advice.

Regards,
Fabian

my favorit hoster is ASPnix : www.aspnix.com !
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-01-04 : 16:53:12
How will the INSERT INTO be performed? Via SELECT or VALUES?

Tara Kizer
aka tduggan
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-01-04 : 17:02:06
Something like this should do it:

insert into MyCountries
(CountryName)
select
a.CountryName
from
-- Table with new data
MyNewCountries a
left join
-- Table you are inserting into
MyCountries b
on a.CountryName = b.CountryName
where
-- make sure key not already in the table
b.CountryName is null



CODO ERGO SUM
Go to Top of Page

fabianus76
Posting Yak Master

191 Posts

Posted - 2006-01-04 : 17:13:39
Hi tkizer,

I want to perform via select.

Hi Michael,

thank you very much!

Regards,
Fabian

my favorit hoster is ASPnix : www.aspnix.com !
Go to Top of Page
   

- Advertisement -