Subject | simple question: returning id in sproc |
---|---|
Author | zamb1zz1 |
Post date | 2004-12-07T06:01:50Z |
I'm rather new to Firebird and can't find a solid example in the
Firebird Book on how to return an ID in a stored procedure.
I simply want to fire an insert and then return the last inserted ID.
When I fire this sproc, nothing is inserted and, obviously, no ID is
returned:
create procedure ins_group
(
group_name varchar(20),
description varchar(100),
rank int
)
returns
(
group_id_ int
)
as
begin
insert into
user_group
values
(
null,
:group_name,
:description,
:rank
);
select GEN_ID(gen_group_id, 0) from RDB$DATABASE into :group_id_;
suspend;
end
^
For example...I executed it w/ this snippet:
execute procedure ins_group
(
'Administrator',
'Root-level access to entire system',
1
);
Here is the trigger and generator for that table's ID field:
CREATE TRIGGER trig_gen_group_id FOR user_group
ACTIVE BEFORE INSERT POSITION 0
as
begin
if (new.group_id is null) then
new.group_id = gen_id(gen_group_id,1);
end
^
...and
CREATE GENERATOR gen_group_id;
Like I said, I don't get an error, I just get zero output and zero
rows affected (using IBOConsole front-end tool).
Thanks!
-v
Firebird Book on how to return an ID in a stored procedure.
I simply want to fire an insert and then return the last inserted ID.
When I fire this sproc, nothing is inserted and, obviously, no ID is
returned:
create procedure ins_group
(
group_name varchar(20),
description varchar(100),
rank int
)
returns
(
group_id_ int
)
as
begin
insert into
user_group
values
(
null,
:group_name,
:description,
:rank
);
select GEN_ID(gen_group_id, 0) from RDB$DATABASE into :group_id_;
suspend;
end
^
For example...I executed it w/ this snippet:
execute procedure ins_group
(
'Administrator',
'Root-level access to entire system',
1
);
Here is the trigger and generator for that table's ID field:
CREATE TRIGGER trig_gen_group_id FOR user_group
ACTIVE BEFORE INSERT POSITION 0
as
begin
if (new.group_id is null) then
new.group_id = gen_id(gen_group_id,1);
end
^
...and
CREATE GENERATOR gen_group_id;
Like I said, I don't get an error, I just get zero output and zero
rows affected (using IBOConsole front-end tool).
Thanks!
-v