
January 16th, 2012, 09:00 AM
|
|
Contributing User
|
|
Join Date: Sep 2006
Location: Plovdiv. Bulgaria
Posts: 200
  
Time spent in forums: 2 Days 6 h 50 m 4 sec
Reputation Power: 11
|
|
Quote: | Originally Posted by rusdyrip thank you for the function but how i detect if the row already
zero?
i don't really powerful on stored procedure query,pls tell me
the solution
thx |
Code:
CREATE OR ALTER TRIGGER TR_TABLE_NAME_AD1101 FOR TABLE_NAME
ACTIVE AFTER DELETE POSITION 1101
AS
DECLARE VARIABLE L_COUNT INTEGER;
BEGIN
SELECT
COUNT(*)
FROM
TABLE_NAME
INTO
:L_COUNTER;
IF (COALESCE(L_COUNTER,0) = 0) THEN
EXECUTE STATEMENT 'ALTER SEQUENCE SEQUENCE_NAME RESTART WITH 0';
END;
But counting all records in a table after each delete is not a good idea. Especially for tables having large number of records.
Another way is to use SELECT FIRST 1. You have to choose one field that always have a value (not null, maybe the field that is the primary key in the table) from the table:
Code:
CREATE OR ALTER TRIGGER TR_TABLE_NAME_AD1101 FOR TABLE_NAME
ACTIVE AFTER DELETE POSITION 1101
AS
DECLARE VARIABLE L_VARIABLE SAME_TYPE_AS_THE_FIELD'S_TYPE;
BEGIN
SELECT FIRST 1
FIELD_NAME
FROM
TABLE_NAME
INTO
:L_VARIABLE;
IF (L_VARIABLE IS NULL) THEN
EXECUTE STATEMENT 'ALTER SEQUENCE SEQUENCE_NAME RESTART WITH 0';
END;
Last edited by mIRCata : January 16th, 2012 at 09:10 AM.
|