Subject | Re: [firebird-support] Returning a value from a Stored Procedure |
---|---|
Author | Rik Barker |
Post date | 2006-05-19T09:59:04Z |
Hi Martijn,
pretty sure I picked up from some of the Firebird or Interbase
documentation I was waist high in yesterday. I'd like to understand better
why this is bad design.
My approach is almost identical, except I return 2 values. An ID and
whether an insert occurred.
PROCEDURE FIND_OR_ADD_ARTIST(
NAME CHAR(100) CHARACTER SET NONE)
RETURNS(
ARTISTID BIGINT,
ADDED SMALLINT)
AS
DECLARE VARIABLE FOUNDID BIGINT;
BEGIN
FoundID = -1;
Added=0;
SELECT ID FROM ARTISTS WHERE (Artists.Name = :NAME) INTO :FoundID;
if (:FoundID = -1) THEN
BEGIN
INSERT INTO Artists (Name) VALUES (:NAME);
SELECT gen_id(ARTISTS_ID_GEN,0) FROM RDB$DATABASE INTO :FoundID;
Added=1;
END
ArtistID = :FoundID;
SUSPEND;
END
Cheers,
Rik
>However, IMO, it's _very_ bad practice to do a "select fromCould you explain that? I'm using an almost identical technique - one I'm
>myprocedure" that updates/inserts data. Only use select-able
>stored procedures for procedures that simply return data without
>modifying it.
pretty sure I picked up from some of the Firebird or Interbase
documentation I was waist high in yesterday. I'd like to understand better
why this is bad design.
My approach is almost identical, except I return 2 values. An ID and
whether an insert occurred.
PROCEDURE FIND_OR_ADD_ARTIST(
NAME CHAR(100) CHARACTER SET NONE)
RETURNS(
ARTISTID BIGINT,
ADDED SMALLINT)
AS
DECLARE VARIABLE FOUNDID BIGINT;
BEGIN
FoundID = -1;
Added=0;
SELECT ID FROM ARTISTS WHERE (Artists.Name = :NAME) INTO :FoundID;
if (:FoundID = -1) THEN
BEGIN
INSERT INTO Artists (Name) VALUES (:NAME);
SELECT gen_id(ARTISTS_ID_GEN,0) FROM RDB$DATABASE INTO :FoundID;
Added=1;
END
ArtistID = :FoundID;
SUSPEND;
END
Cheers,
Rik