Welcome To The Home Of The Visual FoxPro Experts  
home. signup. forum. archives. search. google. articles. downloads. faq. members. weblogs. sponsors. rss.
 From: Vivek Deodhar
  Where is Vivek Deodhar?
 SANGLI
 India
 Vivek Deodhar
 Tags
Subject: Mysql before insert trigger
Thread ID: 229146 Message ID: 229146 # Views: 33 # Ratings: 0
Version: Visual FoxPro 9 SP2 Category: Databases, Tables and SQL Server
Date: Monday, May 25, 2009 4:52:48 PM         
   


Hi,

I want to create incrementing voucher numbers in mysql table as I insert voucher entries.

Table name is le
Column name is vouno (mediumint,not null)

There is another column which is auto incrementing, so I can not use auto increment for column "vouno".

As I do not want to lock the table, I tried to use a "before insert" trigger like this.

delimiter |
CREATE TRIGGER addvouno BEFORE INSERT ON le
FOR EACH ROW BEGIN
DECLARE NewINT MEDIUMINT;
SELECT MAX(vouno)+1 INTO NewINT from le;
if @newint is null then
set @newint=1;
end if;
SET NEW.vouno=@newint;
END;
|
delimiter ;



The first insert gives the value of 1 to vouno.
Strangely, even for all next inserts, value of vouno is inserted as 1 (instead of 2,3 etc.)

The variable newint is always returning value of 1. This means the statement,

"SELECT MAX(vouno)+1 INTO NewINT from le" is always returning null value.

What is wrong in above code?

Vivek



COMPLETE THREAD
Mysql before insert trigger Posted by vivek deodhar @ 5/25/2009 4:52:48 PM
RE: Mysql before insert trigger Posted by Bhushan Sahni @ 5/25/2009 6:09:52 PM
RE: Mysql before insert trigger Posted by vivek deodhar @ 5/26/2009 5:41:22 AM
RE: Mysql before insert trigger Posted by Borislav Borissov @ 5/25/2009 6:17:48 PM
RE: Mysql before insert trigger Posted by vivek deodhar @ 5/26/2009 5:39:31 AM