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 2005 Forums
 Transact-SQL (2005)
 QUERY

Author  Topic 

rajdaksha
Aged Yak Warrior

595 Posts

Posted - 2009-07-13 : 04:39:07
Hi

I have a table with 3 columns
CREATE TABLE #Test (C1 nvarchar(10), C2 nvarchar(50), C3 datetime);

Question :
Column C2 should be unique

If I try to insert duplicate record in the column , it should not throw the error, it should insert other record(s) except duplicate record.

How to achieve this?


NOTE

Table already have one record with c2 column value as ‘abc’

Am trying to insert the following record

Select ‘a’, ’abd’, getdate() union all --1
Select ‘a’, ’abc’, getdate() union all --2
Select ‘a’, ’abe’, getdate() --3


It should not throw any error, but it insert only two record except second one (second one violate)

There should not be any check using the script …..




-------------------------
R..

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-07-13 : 04:42:59
1. Write an INSTEAD OF INSERT trigger
2. Write an AFTER INSERT trigger
3. Write an unique index over c2 column with IGNORE_DUP_KEY attribute on index

You can do any of the three suggestions.



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

rajdaksha
Aged Yak Warrior

595 Posts

Posted - 2009-07-13 : 05:43:41
how it is possible by using trigger

-------------------------
R..
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-07-13 : 06:44:47
With the INSTEAD OF TRIGGER, you can do a NOT EXISTS when inserting the new records.
With the AFTER TRIGGER, you can remove all duplicate records
With the unique index with IGNORE_DUP_KEY, the database will take care of it for you.



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

rajdaksha
Aged Yak Warrior

595 Posts

Posted - 2009-07-13 : 07:03:50
thanks peso

-------------------------
R..
Go to Top of Page
   

- Advertisement -