Firebird SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesFirebird SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 15th, 2012, 09:29 AM
rusdyrip rusdyrip is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 38 rusdyrip User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 55 m 56 sec
Reputation Power: 6
Reset Generator Table using Trigger

hi all,
i want to ask how do i reset generator to 0 after i check
there is no record on table

i want use it on trigger,so after user delete all row on table
the generator is set to 0,
i just now syntax to reset generator to 0 but don't know how to
use it on trigger.

syntax :
ALTER SEQUENCE GEN_MASTERBARANG_ID RESTART WITH 0;

thx

Reply With Quote
  #2  
Old January 16th, 2012, 02:05 AM
mIRCata mIRCata is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2006
Location: Plovdiv. Bulgaria
Posts: 200 mIRCata User rank is Corporal (100 - 500 Reputation Level)mIRCata User rank is Corporal (100 - 500 Reputation Level)mIRCata User rank is Corporal (100 - 500 Reputation Level)mIRCata User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 6 h 50 m 4 sec
Reputation Power: 11
Quote:
Originally Posted by rusdyrip
hi all,
i want to ask how do i reset generator to 0 after i check
there is no record on table

i want use it on trigger,so after user delete all row on table
the generator is set to 0,
i just now syntax to reset generator to 0 but don't know how to
use it on trigger.

syntax :
ALTER SEQUENCE GEN_MASTERBARANG_ID RESTART WITH 0;

thx

The only wat to do this in stored procedure or trigger is with EXECUTE STATEMENT
Code:
EXECUTE STATEMENT 'ALTER SEQUENCE GEN_MASTERBARANG_ID RESTART WITH 0';

Reply With Quote
  #3  
Old January 16th, 2012, 07:21 AM
rusdyrip rusdyrip is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 38 rusdyrip User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 55 m 56 sec
Reputation Power: 6
Quote:
Originally Posted by mIRCata
The only wat to do this in stored procedure or trigger is with EXECUTE STATEMENT
Code:
EXECUTE STATEMENT 'ALTER SEQUENCE GEN_MASTERBARANG_ID RESTART WITH 0';


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

Reply With Quote
  #4  
Old January 16th, 2012, 09:00 AM
mIRCata mIRCata is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2006
Location: Plovdiv. Bulgaria
Posts: 200 mIRCata User rank is Corporal (100 - 500 Reputation Level)mIRCata User rank is Corporal (100 - 500 Reputation Level)mIRCata User rank is Corporal (100 - 500 Reputation Level)mIRCata User rank is Corporal (100 - 500 Reputation Level) 
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.

Reply With Quote
  #5  
Old January 16th, 2012, 09:00 PM
rusdyrip rusdyrip is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 38 rusdyrip User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 55 m 56 sec
Reputation Power: 6
thank you,your solution is the best for my case

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesFirebird SQL Development > Reset Generator Table using Trigger

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap