Re: Sequences
Posted in 2005
Here's an example which does what you want. You have to use both a
trigger and a function:
create table mytab (
tid integer,
txt varchar(10)
);
create sequence myseq start with 10000 maxvalue 19999 cycle;
create function myval()
returning integer; return (select myseq.nextval from systables where tabid=1);
end function;
create trigger mytrig insert on mytab
referencing new as post
for each row when (post.tid is null)
(execute function myval() into tid);
insert into mytab (txt) values ('first');
insert into mytab (txt) values ('second');
insert into mytab (txt) values ('third');
regards
Claus
Colin Bull wrote:
> Is it possible to set up sequences to mimic the actions of a serial
> field ? (IDS 9.4 FC2)
> I want to be able to insert a new record with out changing the
> application code but with more control of the limits of the serial
> field.
>
> I have a table -
> create table tab1 ( tab1_id int8, descrip char(20) ) ;
> alter table tab1 add constraint primary key (tab1_id) constraint> pk_tab1;
>
> And a sequence -
> create sequence tab1_seq start with 10000 maxvalue 19999 cycle ;>
> I would like to run sql -
> insert into tab1 ( descript ) values ("first record" )>
> And to see -
>
> tab1_id descrip
> 10000 first record
>
>
> How can I link one to the other ? The Syntax manual is unclear although
> it mentions synonyms, and the SQL tutorial does not mention them.
>
>
> Colin Bull
>
> _______________________________________________________________________
> This email has been scanned for all known viruses by the MessageLabs Email
> Security System.
> _______________________________________________________________________
>
> sending to informix-list