Subject | Re: [firebird-support] stored procedured with insert ... returning |
---|---|
Author | Mark Rotteveel |
Post date | 2018-06-10T08:03:11Z |
On 10-6-2018 01:14, talorigomat@... [firebird-support] wrote:
1. The :EMPID is a stored procedure variable reference, not a column
reference, and you don't have a variable :EMPID defined.
2. When using INSERT .. RETURNING .. in a stored procedure, you must use
INSERT .. RETURNING .. INTO ..
3. When returning values from a stored procedure, you must explicitly
declare the return values (assuming you want to return it from the
stored procedure).
In other words, change your code to:
SET TERM ! ;
CREATE OR ALTER PROCEDURE TEST (
ATITLE DOM_VARCHARTINY,
AFIRST_NAME DOM_VARCHARMEDIUM,
AINITIALS DOM_VARCHARTINY,
ASURNAME DOM_VARCHARMEDIUM,
AEMAIL DOM_VARCHARLARGE,
APHONE DOM_VARCHARTINY)
returns (aempid bigint) -- or whatever data type empid is
AS
begin
INSERT INTO Employee (title, first_name, initials, surname,
email, phone_ext)
VALUES(:ATitle, :AFirst_Name, :AInitials, :ASurname, :AEmail,
:APhone)
RETURNING empid into :aempid;
end !
SET TERM ; !
BTW: Why define this stored procedure? You can just as well use the
insert directly.
If you just want to use the returned id with the stored procedure, do
something like:
SET TERM ! ;
CREATE OR ALTER PROCEDURE TEST (
ATITLE DOM_VARCHARTINY,
AFIRST_NAME DOM_VARCHARMEDIUM,
AINITIALS DOM_VARCHARTINY,
ASURNAME DOM_VARCHARMEDIUM,
AEMAIL DOM_VARCHARLARGE,
APHONE DOM_VARCHARTINY)
AS
declare aempid bigint; -- or whatever data type empid is
begin
INSERT INTO Employee (title, first_name, initials, surname,
email, phone_ext)
VALUES(:ATitle, :AFirst_Name, :AInitials, :ASurname, :AEmail,
:APhone)
RETURNING empid into :aempid;
end !
SET TERM ; !
--
Mark Rotteveel
> When I try to create the following stored procedure I get sql error codeYour code has a number of problems:
> = -206 Column unknown EMPID
>
>
> SET TERM ! ;
> CREATE OR ALTER PROCEDURE TEST (
> ATITLE DOM_VARCHARTINY,
> AFIRST_NAME DOM_VARCHARMEDIUM,
> AINITIALS DOM_VARCHARTINY,
> ASURNAME DOM_VARCHARMEDIUM,
> AEMAIL DOM_VARCHARLARGE,
> APHONE DOM_VARCHARTINY)
> AS
> begin
> INSERT INTO Employee (title, first_name, initials, surname,
> email, phone_ext)
> VALUES(:ATitle, :AFirst_Name, :AInitials, :ASurname, :AEmail,
> :APhone)
> RETURNING :EMPID;
> end !
> SET TERM ; !
>
> The table does have a EMPID c olumn defined and if I type the insert
> statement and execute it, it does work.
1. The :EMPID is a stored procedure variable reference, not a column
reference, and you don't have a variable :EMPID defined.
2. When using INSERT .. RETURNING .. in a stored procedure, you must use
INSERT .. RETURNING .. INTO ..
3. When returning values from a stored procedure, you must explicitly
declare the return values (assuming you want to return it from the
stored procedure).
In other words, change your code to:
SET TERM ! ;
CREATE OR ALTER PROCEDURE TEST (
ATITLE DOM_VARCHARTINY,
AFIRST_NAME DOM_VARCHARMEDIUM,
AINITIALS DOM_VARCHARTINY,
ASURNAME DOM_VARCHARMEDIUM,
AEMAIL DOM_VARCHARLARGE,
APHONE DOM_VARCHARTINY)
returns (aempid bigint) -- or whatever data type empid is
AS
begin
INSERT INTO Employee (title, first_name, initials, surname,
email, phone_ext)
VALUES(:ATitle, :AFirst_Name, :AInitials, :ASurname, :AEmail,
:APhone)
RETURNING empid into :aempid;
end !
SET TERM ; !
BTW: Why define this stored procedure? You can just as well use the
insert directly.
If you just want to use the returned id with the stored procedure, do
something like:
SET TERM ! ;
CREATE OR ALTER PROCEDURE TEST (
ATITLE DOM_VARCHARTINY,
AFIRST_NAME DOM_VARCHARMEDIUM,
AINITIALS DOM_VARCHARTINY,
ASURNAME DOM_VARCHARMEDIUM,
AEMAIL DOM_VARCHARLARGE,
APHONE DOM_VARCHARTINY)
AS
declare aempid bigint; -- or whatever data type empid is
begin
INSERT INTO Employee (title, first_name, initials, surname,
email, phone_ext)
VALUES(:ATitle, :AFirst_Name, :AInitials, :ASurname, :AEmail,
:APhone)
RETURNING empid into :aempid;
end !
SET TERM ; !
--
Mark Rotteveel