从选择查询创建过程

时间:2014-03-26 15:56:29

标签: sql stored-procedures firebird2.5

我需要在Firebird 2.5中编写存储过程。我写了这个SQL查询:

INSERT INTO A_OBRATYKUMHIST(OBRAT, MONTH, YEAR, SECURITYUSER_ID, FIRM_ID)
select 
    sum(AO.Obrat), 11, 2010, AO.SecurityUser_ID, AO.Firm_ID
from A_OBRATYKUMHIST_TEMP AO
where
    AO.Rok = 2010
    and AO.Mesic <= 11
group by
    AO.SecurityUser_ID, AO.Firm_ID;

我的目标是填表,累积金额。

编辑1:

所以我写了这个:

SET TERM ^ ;

CREATE OR ALTER PROCEDURE A_TESTIK (
    start_year integer,
    end_year integer)
returns (
    obrat integer,
    mesic integer,
    rok integer)
as
declare variable "YEAR" integer = 2005;
declare variable "MONTH" integer = 1;
begin
    select
        sum(II.localamountwithoutvat), ib_decodedate_month(VatDate$DATE),
            ib_decodedate_month(VatDate$DATE)
    from IssuedInvoices II
    group by
        ib_decodedate_month(VatDate$DATE), ib_decodedate_month(VatDate$DATE)
    into :obrat, :mesic, :rok;
    suspend;
end^

SET TERM ; ^

/* Following GRANT statetements are generated automatically */

GRANT SELECT ON ISSUEDINVOICES TO PROCEDURE A_TESTIK;

/* Existing privileges on this procedure */

GRANT EXECUTE ON PROCEDURE A_TESTIK TO SYSDBA;

但是当我运行它时,我收到错误:"Multiple rows in singleton select"。我该如何解决?

2 个答案:

答案 0 :(得分:1)

以下是Firebird proc语法:

CREATE PROCEDURE name [(param1 datatype1, param2 datatype2, ...)] 
[RETURNS (param3 datatype3, param4 datatype4, ...)] 
AS BEGIN
  <body>
END; 

剩下的由你决定!

Here is some good material about procs in Firebird

答案 1 :(得分:1)

如果SELECT返回多行,则需要使用FOR SELECT ... DO ...

将存储过程的主体更改为:

FOR select
       sum(II.localamountwithoutvat), ib_decodedate_month(VatDate$DATE),
        ib_decodedate_month(VatDate$DATE)
   from IssuedInvoices II
   group by
       ib_decodedate_month(VatDate$DATE), ib_decodedate_month(VatDate$DATE)
   into :obrat, :mesic, :rok
DO
   suspend;