-1

I have a package which has 4 parameters. 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. 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!

Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134
user2181349
  • 65
  • 2
  • 9

3 Answers3

2

You should handle the if else logic in ref cursor

Inside a normal cursor, you cannot do if/else. So declare a ref cursor and then your logic should work. See below link.

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

Utsav
  • 7,914
  • 2
  • 17
  • 38
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.

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).

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;
Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134
  • i am developing a concurrent report. Which has P_TranType parameter. There are different datasets for different trantype parameter. when trantype paramener is passed suppose say its 'AP' then it should redirect to the respective select statement in the package based . for that i am checking for if else statement. If any better alternatives please suggest. – user2181349 Sep 24 '15 at 12:01
0

I think you should use dynamic sql statements for this approach. 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 ...