Google
 

Kamis, 05 Juli 2007

Generator (FB) vs Sequence(Oracle)

In this comment I will compare a featured called "generator" in Firebird (FB) with "sequence" in Oracle. Both have the same function that for generate an automatic number. FB give us an easy way to reset or change the value of generator through the "gen_id(generator_name, increment_value)" function.
Example: "select gen_id(gen_tb_barang, 1) from rdb$database"

This "gen_id" function is also very flexible that we can reset the generator value to zero.
Example: "select gen_id(gen_tb_barang, -gen_id(gen_tb_barang,0)) from rdb$database"
And we can also get last generator value with this SQL:
Example: "select gen_id(gen_tb_barang, 0) from rdb$database"

We can get the value of sequence in Oracle with "select sq_tb_barang.nextval from dual". The "nextval" method is increment by 1 by default, we can change the increment step by this SQL command "alter sequence increment by ".

To reset sequence to become zero in Oracle is more complex, we can drop that sequence and then create it again for simple but other say this (although I never success with this procedure to make my sequence become zero):

The logic is:
1) get the curval from the sequence
2) alter the sequence to have increment as the curval * -1
3) get the nextval from the sequence
4) alter the sequnce to have increment as 1

The code is:

declare Handle Integer;
SqlText varchar2(2000);
Temp Integer;
Begin
select sq_barang.nextval into temp from dual;
temp := temp * -1;
SqlText := 'Alter Sequence sq_barang increment by ' || to_char(temp);
Handle := dbms_sql.open_cursor;
dbms_sql.parse(handle, SqlText, DBMS_SQL.NATIVE);
select sq_barang.nextval into temp from dual;
SqlText := 'Alter Sequence sq_barang increment by 1' ;
dbms_sql.parse(handle, SqlText, DBMS_SQL.NATIVE);
End;

Tidak ada komentar: