If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Treating a unique constraint in a procedure. Custom errors.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-29-04, 09:56
AStefan AStefan is offline
Registered User
 
Join Date: Jun 2004
Posts: 57
Treating a unique constraint in a procedure. Custom errors.

I have a table Table1 with three fields:
_PK, _Unique and _Description
The field _PK is primary key, and the field _Unique is constraint to be unique.
The _PK is generated with a sequence. In SQL Server the procedure is something like that (using a custom error)

create procedure Insert_Table1 (v_Unique ..., v_Description ...)
as
if exists (select * from Table1 where _Unique = v_Unique)
raiserror (@Custom_error, 16, 1)
else
insert into TAble1 (_UNique, _Description)
select v_Unique, v__Description

Can you tell me please how can I translate this procedure in a db2 sql procedure? Can I define a custom error?
Thanks.
Reply With Quote
  #2 (permalink)  
Old 06-29-04, 10:23
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Try something like:

create procedure Insert_Table1 (v_Unique ..., v_Description ...)
...

if exists (select * from Table1 where _Unique = v_Unique)
THEN SIGNAL SQLSTATE '75001' SET MESSAGE_TEXT = 'Value exists'
else
insert into TAble1 (_UNique, _Description) values (v_Unique, v__Description );
END IF;

Andy
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On