Subject | Re: How to populate a new table column with a INTEGER sequence? |
---|---|
Author | |
Post date | 2014-09-22T12:36:22Z |
Hi to all,
This is one approach (Maybe could be done one more efficient):
/**
* Firebird 2.5
*/
-- 1.Create new fields
ALTER TABLE some_table
ADD id INTEGER DEFAULT 0 NOT NULL,
ADD foo_dummy CHAR(1);
-- 2.Create generador
CREATE GENERATOR some_table_id_gen;
-- 3.Create temp trigger
SET TERM ^ ;
CREATE TRIGGER some_table_temp_trgr FOR some_table
ACTIVE BEFORE UPDATE POSITION 0
AS
BEGIN
new.id = GEN_ID(some_table_id_gen, 1);
END^
SET TERM ; ^
-- 4.Update foo_dummy field and so set the sequency
-- into field ID
UPDATE some_table SET foo_dummy = '1';
-- 5.Drop temp trigger and foo_dummy
DROP TRIGGER some_table_temp_trgr;
ALTER TABLE some_table DROP foo_dummy;
-- 6.Drop previous non ID primary key
ALTER TABLE some_table DROP CONSTRAINT some_table_current_pk;
-- 7.Create new PK
ALTER TABLE some_table ADD CONSTRAINT some_table_pk PRIMARY KEY (ID);
-- 9.Create new trigger for mantaining new PK
SET TERM !! ;
CREATE TRIGGER some_table_set_id FOR some_table
BEFORE INSERT POSITION 0 AS
BEGIN
NEW.id = GEN_ID(some_table_id_gen, 1);
END !!
SET TERM ; !!
/* */
Best regards,
Hernando.