2014-03-26 129 views
0

我需要在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"。我該如何解決它?

+0

閱讀材料:Interbase的6.0語言參考] (http://www.ibphoenix.com/files/60LangRef.zip)(特別是第3章程序和觸發器)和[Firebird 2.5語言參考更新](http://www.firebirdsql.org/file/documentation/reference_manuals/參考, nce_material/html/langrefupd25.html) –

+0

如果這沒有幫助,請更具體地說明你想知道的內容。 –

+0

@Mihai 有用的評論 – Sk1X1

回答

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; 
+0

不錯,謝謝! – Sk1X1

1

這裏是火鳥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

+0

謝謝,我會檢查它。 – Sk1X1

+0

如果我的答案送達!將答案標記爲已回答! Up_One給我! –

+0

我編輯了我的問題:) – Sk1X1