Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

return status/error code do to violation of unique key con..

 
   Database Help (Home) -> Programming RSS
Next:  Table size and dates  
Author Message
Andy B.

External


Since: Feb 17, 2009
Posts: 6



(Msg. 1) Posted: Tue Mar 17, 2009 8:42 am
Post subject: return status/error code do to violation of unique key constraint
Archived from groups: microsoft>public>sqlserver>programming (more info?)

One of my tables has a unique key on the Title column. I have a stored
procedure that inserts a row into the table. I want to return a series of
status/error codes depending on what happened in the code. The successful
insert isn't much of a problem but I need to catch certain
exceptions/errors. One of them is the violation of a unique key. How do I do
this for one and for two, how would you be able to tell what unique key it
was that got violated (in case there is more than 1 on a single table)?

 >> Stay informed about: return status/error code do to violation of unique key con.. 
Back to top
Login to vote
Plamen Ratchev

External


Since: Aug 20, 2008
Posts: 672



(Msg. 2) Posted: Tue Mar 17, 2009 9:16 am
Post subject: Re: return status/error code do to violation of unique key constraint [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

You can always check if the key values exist before insert and report
back those that violate the uniqueness:

SELECT keycol
FROM Target AS T
WHERE EXISTS(SELECT *
FROM Source AS S
WHERE S.keycol = T.keycol);

or:

SELECT keycol FROM Target
INTERSECT
SELECT keycol FROM Source;

--
Plamen Ratchev
http://www.SQLStudio.com

 >> Stay informed about: return status/error code do to violation of unique key con.. 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
sysprocesses.status always return "sleeping" - Hi With SQL Server 2000, I have created a profiler with DB_ID = x, every things is ok and I can see running queries, But if I query sysprocesses with query like this : select sql_handle, spid, status, last_batch, waitresource from..

SSIS hitting FK violation - need to ignore the error rows .. - I'm using SSIS and i'm getting a FK violation in my OLE DB Destination task. I'm getting a FK violation and would like to ignore any rows (throw them away) where the error is encountered. The Destination objects doesn't seem able to deal with specific....

Generating unique nvarchar unique key - Hi, Would like to know whether it is possible to generate a unique key for a nvarchar column. This key will need to generate by a T-SQL Stored Proc, the format as follow: XX-ZZZZ XX is month, where I think it should be able to generate using..

Error in SQL code - There is an error within my SQL code that I just can't pick up, does anyone by any chance know what it is, any help would be greatly appreciated, my statement is below: select a.index_id, a.index_name, c.index_date, c.close_index_value, b.index_id,..

Selecting from a table where my unique Key is not their un.. - I've got a list of customers, customer names, ... from another db (I didn't design it). I want to get a list of all of the customers plus the first customer name. So if customer 1 appears twice (customer name = 'Fred' and 'John') I want the first one...
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]