简体   繁体   English

打包过程中的PLSQL IF ELSE

[英]PLSQL IF ELSE in Package procedure

I have a package which has 4 parameters. 我有一个包含4个参数的程序包。 All the parameters will come from concurrent request. 所有参数将来自并发请求。 In that based on transaction type parameter the package should execute the set of statements.I just started with oracle. 在这种基于事务类型参数的方法中,程序包应执行一组语句。我刚开始使用oracle。 Below is my code 下面是我的代码

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;

Will my approach work? 我的方法行得通吗? Please help me to get this done! 请帮助我完成此任务!

You should handle the if else logic in ref cursor 您应该在ref游标中处理if else逻辑

Inside a normal cursor, you cannot do if/else. 在普通光标内,您不能执行if / else。 So declare a ref cursor and then your logic should work. 因此,声明一个ref游标,然后您的逻辑应该起作用。 See below link. 见下面的链接。

https://community.oracle.com/thread/2237472?tstart=0 https://community.oracle.com/thread/2237472?tstart=0

You make the mistake of having a conditional in your declaration block. 您在声明块中犯了一个有条件的错误。 If you've at least compiled it, Oracle would have thrown you the error message. 如果您至少已对其进行编译,则Oracle会向您抛出错误消息。

Your actual implementation is messy and honestly not sure what you're doing. 您的实际实现是混乱的,说实话,您不确定自己在做什么。 Even if you do move the IF block from the declaration to the executable section, you will another compile error because a SELECT statement needs to be fetched into local variables(which you haven't done) and then might get a runtime exception since SQL statements must return a single row (and I suspect your SQL statement doesn't do what it is supposed to do). 即使您确实将IF块从声明移到可执行部分,也将产生另一个编译错误,因为需要将SELECT语句提取到局部变量中(您尚未完成),然后由于SQL语句可能会获得运行时异常必须返回一行(我怀疑您的SQL语句没有执行应做的事情)。

This is what it should probably look as, but without explaining what you're trying to do, this is just a shot in the darl 这可能看起来应该是这样,但是在没有解释您要尝试做的事情的情况下,这只是一头雾水

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;

I think you should use dynamic sql statements for this approach. 我认为您应该为这种方法使用动态sql语句。 like: 喜欢:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM