Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trigger does not work properly

Status
Not open for further replies.

Antonov

Programmer
Aug 1, 2001
3
RU
Hello !
I have very simple SQL script.

create table T1(
ID integer DEFAULT 1,
Name varchar(20),
primary key(ID)
)#
create table T2(
ID integer DEFAULT 1,
Name varchar(20)
)#
CREATE TRIGGER InsT1 AFTER INSERT ON T1 ORDER 1 FOR EACH ROW
INSERT INTO T2 VALUES(NEW.ID,NEW.Name);#
CREATE TRIGGER PrimT1 BEFORE INSERT ON T1 ORDER 1 FOR EACH ROW
DECLARE :ID INTEGER;
SELECT IFNULL(MAX(ID),0)+1 INTO :ID FROM T1;
SET NEW.ID=:ID;#
CREATE PROCEDURE InsT (IN :C INTEGER);
DECLARE :I UINTEGER = 0;
BEGIN
WHILE :)I < :C) DO
SET :I = :I + 1;
INSERT INTO T1 VALUES(1,'1');
END WHILE;
END#

When I execute &quot;CALL InsT(1)&quot;, I have no errors:
&quot;SQL statement(script) has executed successfully.&quot; message
But after execution of &quot;CALL InsT(2)&quot; I've got next error
&quot;ODBC Error: SQLSTATE = S1000, Native error code = -5099
The record has a key field containing a duplicate value(Btrieve Error 5)&quot;

Can anybody explain me that it is wrong?
 
Looks like a typo...in your insert statement in Procedure InsT you are inserting a stated value rather than using the variable :I that you created.

you area using ... INSERT INTO T1 VALUES(1,'1');
rather than .... INSERT INTO T1 VALUES:)I,'1');

Hope this helps.
-Mark --Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top