Subject | Re: [firebird-support] Trigger Help |
---|---|
Author | Helen Borrie |
Post date | 2003-12-16T23:06:46Z |
At 11:39 AM 16/12/2003 -0500, you wrote:
possibility of bad input, you won't know whether they will do something or not.
variable is being used in a DSQL statement */
adding 1 to it still makes it null. Of course, nothing will happen if
UseID isn't 1 */
in Stations */
CREATE EXCEPTION INVALID_STATION 'Invalid station data';
COMMIT;
CREATE TRIGGER INSERTEXTORDERID FOR ORDERS
ACTIVE BEFORE INSERT POSITION 0
AS
DECLARE VARIABLE UseID SmallInt;
DECLARE VARIABLE LastExtOrderID Integer;
BEGIN
if (not exists (
SELECT 1 FROM STATIONS
WHERE STationID = NEW.StationID) then
exception INVALID_INPUT;
else
begin
SELECT UseExtOrderID, ExtOrderID
FROM STATIONS
WHERE STationID = NEW.StationID
INTO :UseID, :LastExtOrderID;
if ((UseID is null) or (LastExtOrderID is null)) then
exception INVALID_STATION;
else
begin
If (UseID = 1) THEN
begin
New.ExtOrderID = LastExtOrderID + 1;
UPDATE Stations
SET Stations.ExtOrderID = Stations.ExtOrderID + 1
WHERE Stations.StationID = New.StationID;
end
end
end
END
/heLen
>Hello all,Triggers always fire. The problem is, if you don't code for the
>
>I am using the following trigger to update a value in the "Orders" table
>and the "Stations" table in my database. I know that it appears not to
>be multi user safe, but rules in the client app prevent any two person
>from being logged onto a particular "Station" which provides the
>"StationID" field below. The problem is that the trigger does not seem
>to be firing.
possibility of bad input, you won't know whether they will do something or not.
>CREATE TRIGGER INSERTEXTORDERID FOR ORDERS/* at this point both variables are null */
>ACTIVE BEFORE INSERT POSITION 0
>AS
> DECLARE VARIABLE UseID SmallInt;
> DECLARE VARIABLE LastExtOrderID Integer;
>BEGIN/*Won't find anything if StationID is invalid, won't except either */
> Begin
> SELECT UseExtOrderID, ExtOrderID
> FROM STATIONS
> WHERE STationID = NEW.StationID
> INTO :UseID, :LastExtOrderID;/* variables remain null if row was not found */
> If (:UseID = 1) THEN/* Take the colon off the variable reference: use the colon only when the
variable is being used in a DSQL statement */
> begin/* Again, remove colon. But, even if UseID is 1, if variable is null then
> New.ExtOrderID = :LastExtOrderID + 1;
adding 1 to it still makes it null. Of course, nothing will happen if
UseID isn't 1 */
> UPDATE Stations SET Stations.ExtOrderID =/* same story as above - nothing will happen if new.StationID isn't present
>
>
> Stations.ExtOrderID + 1 WHERE Stations.StationID = New.StationID;
in Stations */
> endCREATE EXCEPTION INVALID_INPUT 'Invalid input';
> end
>
>END
CREATE EXCEPTION INVALID_STATION 'Invalid station data';
COMMIT;
CREATE TRIGGER INSERTEXTORDERID FOR ORDERS
ACTIVE BEFORE INSERT POSITION 0
AS
DECLARE VARIABLE UseID SmallInt;
DECLARE VARIABLE LastExtOrderID Integer;
BEGIN
if (not exists (
SELECT 1 FROM STATIONS
WHERE STationID = NEW.StationID) then
exception INVALID_INPUT;
else
begin
SELECT UseExtOrderID, ExtOrderID
FROM STATIONS
WHERE STationID = NEW.StationID
INTO :UseID, :LastExtOrderID;
if ((UseID is null) or (LastExtOrderID is null)) then
exception INVALID_STATION;
else
begin
If (UseID = 1) THEN
begin
New.ExtOrderID = LastExtOrderID + 1;
UPDATE Stations
SET Stations.ExtOrderID = Stations.ExtOrderID + 1
WHERE Stations.StationID = New.StationID;
end
end
end
END
/heLen