[英]PLSQL IF ELSE in Package procedure
我有一個包含4個參數的程序包。 所有參數將來自並發請求。 在這種基於事務類型參數的方法中,程序包應執行一組語句。我剛開始使用oracle。 下面是我的代碼
CREATE OR replace PACKAGE BODY
vat_reg_6
IS
PROCEDURE xx_po_vat_reg_proc_par(errbuf OUT VARCHAR2,
retcode OUT VARCHAR2,
p_startdate DATE,
p_enddate DATE,
p_legal_entity_id NUMBER,
p_trantype VARCHAR2)
IS
CURSOR po_cursor IS
--============================My Approach=================
IF p_trantype='AP'-- Should execute below block
SELECT al.tax_rate_code AS taxcode,
al.amount AS netamount,
al.amount AS taxamount,
ai.invoice_date AS reportingdate,
ai.invoice_num AS invoicenumber,
ai.invoice_date AS invoicedate,
ai.invoice_amount AS grossamount,
ai.invoice_num AS documentnumber ,
ai.invoice_date AS documentdate,
ai.vendor_id AS suplierid,
hz.tax_reference AS suppliervatnumber,
gl.segment1 AS companycode,
'AP' AS transactiontype
FROM apps.ap_invoice_lines_all al
inner join apps.ap_invoices_all ai
ON ai.invoice_id=al.invoice_id
inner join apps.hz_parties hz
ON ai.party_id=hz.party_id
inner join apps.ap_invoice_distributions_all dl
ON dl.invoice_id=al.invoice_id
inner join apps.gl_code_combinations gl
ON gl.code_combination_id=dl.dist_code_combination_id
WHERE ROWNUM<200
AND ai.invoice_date BETWEEN p_startdate AND p_enddate
AND ai.legal_entity_id=p_legal_entity_id;
ELSE -------------------
--===========This block=====================
BEGIN
/*apps.fnd_file.put_line (
apps.fnd_file.Output,
'Program Started
'
);*/
apps.fnd_file.put_line (apps.fnd_file.output,rpad('TaxCode',8)
|| rpad('NetAMount',15)
|| rpad('TaxAmount',15)
|| rpad('ReportingDate',20)
|| rpad('InvoiceNumber',20)
|| rpad('InvoiceDate',20)
|| rpad('GrossAmount',20)
|| rpad('DocumentNumber',20)
|| rpad('DocumentDate',20)
|| rpad('SuplierID',20)
|| rpad ('SupplierVATNumber',20)
|| rpad('CompanyCode',20)
|| rpad('TransactionType',20));
apps.fnd_file.put_line (apps.fnd_file.output, '--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------' );
/*FND_FILE.put_line(FND_FILE.output,'Starting processing:');*/
FOR po_rec IN po_cursor
LOOP
apps.fnd_file.put_line (apps.fnd_file.output, rpad(po_rec.taxcode,8)
|| rpad(po_rec.netamount,15)
|| rpad(po_rec.taxamount,15)
|| rpad(po_rec.reportingdate,20)
|| rpad(po_rec.invoicenumber,20)
|| rpad(po_rec.invoicedate,20)
|| rpad(po_rec.grossamount,20)
|| rpad(po_rec.documentnumber,20)
|| rpad(po_rec.documentdate,20)
|| rpad(po_rec.suplierid,20)
|| rpad (po_rec.suppliervatnumber,20)
|| rpad(po_rec.companycode,20)
|| rpad(po_rec.transactiontype,20));
/*APPS.FND_FILE.put_line(APPS.FND_FILE.output,
po_rec.TaxCode || po_rec.NetAMount ||
po_rec.TaxAmount || po_rec.ReportingDate||po_rec.InvoiceNumber||po_rec.GrossAmount||po_rec.DocumentNumber||po_rec.DocumentDate||po_rec.SuplierID||
po_rec.SupplierVATNumber||po_rec.CompanyCode||po_rec.TransactionType);*/
/*INSERT INTO APPS_RO.VAT_TEMP VALUES (po_rec.TaxCode,
po_rec.NetAMount,
po_rec.TaxAmount,
po_rec.ReportingDate,
po_rec.InvoiceNumber,
po_rec.InvoiceDate,
po_rec.GrossAmount,
po_rec.DocumentNumber,
po_rec.DocumentDate,
po_rec.SuplierID,
po_rec.SupplierVATNumber,
po_rec.CompanyCode,
po_rec.TransactionType);*/
END LOOP;
--FND_FILE.put_line(FND_FILE.output,'Done!');
COMMIT;
-- Return 0 for successful completion.
errbuf :='';
retcode := '0';
/*exception
when others then
errbuf := sqlerrm;
retcode := '2';*/
END xx_po_vat_reg_proc_par;
END vat_reg_6;
我的方法行得通嗎? 請幫助我完成此任務!
您應該在ref游標中處理if else邏輯
在普通光標內,您不能執行if / else。 因此,聲明一個ref游標,然后您的邏輯應該起作用。 見下面的鏈接。
您在聲明塊中犯了一個有條件的錯誤。 如果您至少已對其進行編譯,則Oracle會向您拋出錯誤消息。
您的實際實現是混亂的,說實話,您不確定自己在做什么。 即使您確實將IF塊從聲明移到可執行部分,也將產生另一個編譯錯誤,因為需要將SELECT語句提取到局部變量中(您尚未完成),然后由於SQL語句可能會獲得運行時異常必須返回一行(我懷疑您的SQL語句沒有執行應做的事情)。
這可能看起來應該是這樣,但是在沒有解釋您要嘗試做的事情的情況下,這只是一頭霧水
CREATE OR REPLACE PACKAGE BODY vat_reg_6 IS
PROCEDURE xx_po_vat_reg_proc_par(errbuf OUT VARCHAR2,
retcode OUT VARCHAR2,
p_startdate DATE,
p_enddate DATE,
p_legal_entity_id NUMBER,
p_trantype VARCHAR2) IS
CURSOR po_cursor IS
SELECT al.tax_rate_code AS taxcode,
al.amount AS netamount,
al.amount AS taxamount,
ai.invoice_date AS reportingdate,
ai.invoice_num AS invoicenumber,
ai.invoice_date AS invoicedate,
ai.invoice_amount AS grossamount,
ai.invoice_num AS documentnumber,
ai.invoice_date AS documentdate,
ai.vendor_id AS suplierid,
hz.tax_reference AS suppliervatnumber,
gl.segment1 AS companycode,
'AP' AS transactiontype
FROM apps.ap_invoice_lines_all al
INNER JOIN apps.ap_invoices_all ai
ON ai.invoice_id = al.invoice_id
INNER JOIN apps.hz_parties hz
ON ai.party_id = hz.party_id
INNER JOIN apps.ap_invoice_distributions_all dl
ON dl.invoice_id = al.invoice_id
INNER JOIN apps.gl_code_combinations gl
ON gl.code_combination_id = dl.dist_code_combination_id
WHERE ROWNUM < 200
AND ai.invoice_date BETWEEN p_startdate AND p_enddate
AND ai.legal_entity_id = p_legal_entity_id;
BEGIN
--============================My Approach=================
IF p_trantype = 'AP' THEN -- Should execute below block
SELECT al.tax_rate_code AS taxcode,
al.amount AS netamount,
al.amount AS taxamount,
ai.invoice_date AS reportingdate,
ai.invoice_num AS invoicenumber,
ai.invoice_date AS invoicedate,
ai.invoice_amount AS grossamount,
ai.invoice_num AS documentnumber,
ai.invoice_date AS documentdate,
ai.vendor_id AS suplierid,
hz.tax_reference AS suppliervatnumber,
gl.segment1 AS companycode,
'AP' AS transactiontype
-- INTO
FROM apps.ap_invoice_lines_all al
INNER JOIN apps.ap_invoices_all ai
ON ai.invoice_id = al.invoice_id
INNER JOIN apps.hz_parties hz
ON ai.party_id = hz.party_id
INNER JOIN apps.ap_invoice_distributions_all dl
ON dl.invoice_id = al.invoice_id
INNER JOIN apps.gl_code_combinations gl
ON gl.code_combination_id = dl.dist_code_combination_id
WHERE ROWNUM < 200
AND ai.invoice_date BETWEEN p_startdate AND p_enddate
AND ai.legal_entity_id = p_legal_entity_id;
ELSE
apps.fnd_file.put_line(
apps.fnd_file.output,
RPAD('TaxCode', 8)
|| RPAD('NetAMount', 15)
|| RPAD('TaxAmount', 15)
|| RPAD('ReportingDate', 20)
|| RPAD('InvoiceNumber', 20)
|| RPAD('InvoiceDate', 20)
|| RPAD('GrossAmount', 20)
|| RPAD('DocumentNumber', 20)
|| RPAD('DocumentDate', 20)
|| RPAD('SuplierID', 20)
|| RPAD('SupplierVATNumber', 20)
|| RPAD('CompanyCode', 20)
|| RPAD('TransactionType', 20));
apps.fnd_file.put_line(
apps.fnd_file.output,
'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------');
/*FND_FILE.put_line(FND_FILE.output,'Starting processing:');*/
FOR po_rec IN po_cursor LOOP
apps.fnd_file.put_line(
apps.fnd_file.output,
RPAD(po_rec.taxcode, 8)
|| RPAD(po_rec.netamount, 15)
|| RPAD(po_rec.taxamount, 15)
|| RPAD(po_rec.reportingdate, 20)
|| RPAD(po_rec.invoicenumber, 20)
|| RPAD(po_rec.invoicedate, 20)
|| RPAD(po_rec.grossamount, 20)
|| RPAD(po_rec.documentnumber, 20)
|| RPAD(po_rec.documentdate, 20)
|| RPAD(po_rec.suplierid, 20)
|| RPAD(po_rec.suppliervatnumber, 20)
|| RPAD(po_rec.companycode, 20)
|| RPAD(po_rec.transactiontype, 20));
/*APPS.FND_FILE.put_line(APPS.FND_FILE.output,
po_rec.TaxCode || po_rec.NetAMount ||
po_rec.TaxAmount || po_rec.ReportingDate||po_rec.InvoiceNumber||po_rec.GrossAmount||po_rec.DocumentNumber||po_rec.DocumentDate||po_rec.SuplierID||
po_rec.SupplierVATNumber||po_rec.CompanyCode||po_rec.TransactionType);*/
/*INSERT INTO APPS_RO.VAT_TEMP VALUES (po_rec.TaxCode,
po_rec.NetAMount,
po_rec.TaxAmount,
po_rec.ReportingDate,
po_rec.InvoiceNumber,
po_rec.InvoiceDate,
po_rec.GrossAmount,
po_rec.DocumentNumber,
po_rec.DocumentDate,
po_rec.SuplierID,
po_rec.SupplierVATNumber,
po_rec.CompanyCode,
po_rec.TransactionType);*/
END LOOP;
--FND_FILE.put_line(FND_FILE.output,'Done!');
END IF;
COMMIT;
-- Return 0 for successful completion.
errbuf := '';
retcode := '0';
/*exception
when others then
errbuf := sqlerrm;
retcode := '2';*/
--FND_FILE.put_line(FND_FILE.output,'Done!');
COMMIT; -- Return 0 for successful completion.
errbuf := '';
retcode := '0';
/*exception
when others then
errbuf := sqlerrm;
retcode := '2';*/
END xx_po_vat_reg_proc_par;
END vat_reg_6;
我認為您應該為這種方法使用動態sql語句。 喜歡:
declare
sql_stmt varchar2(4000);
rec refcursor;
begin
IF p_trantype='AP'THEN -- Should execute below block
sql_stmt:= 'SELECT al.tax_rate_code AS taxcode,
al.amount AS netamount,
al.amount AS taxamount,
ai.invoice_date AS reportingdate,
ai.invoice_num AS invoicenumber,
ai.invoice_date AS invoicedate,
ai.invoice_amount AS grossamount,
ai.invoice_num AS documentnumber,
ai.invoice_date AS documentdate,
ai.vendor_id AS suplierid,
hz.tax_reference AS suppliervatnumber,
gl.segment1 AS companycode,
'AP' AS transactiontype
from...';
else
sql_stmt:=...
end if;
open rec for sql_stmt;
fetch rec into ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.