separated values using Oracle procedure

问题描述 投票:0回答:1

I am trying this procedure. I want to generate csv using oracle procedure table contains more than 500k records. Database = Oracle 12c

    SELECT
        *
    FROM
        test_date;

    CREATE OR REPLACE PROCEDURE genratedata (
        to_date   NVARCHAR2,
        curr      SYS_REFCURSOR
    ) AS
    BEGIN
        OPEN curr FOR SELECT
                          product_name   AS "PRODUCT",
                          expiry_date    AS "END DATE"
                      FROM
                          test_date
                      WHERE
                          expiry_date <= to_date;

    END;
oracle csv processing procedure bulk
1个回答
1
投票

You can use SQL*Plus (as of 12.2) feature to do this:

SQL> select * from t;

     X Y          Z
---------- ---------- --------------------
     1 A          a
   100 A Z        a z

SQL> set markup csv on delimiter | quote off;
SQL> set heading off 
SQL> select * from t;
1|A|a
100|A Z|a z

SQL> 

1
投票

You must use the SQL*Plus to fulfill your task as given in another answer, But if there is a limitation of using the procedure then you must concat each column as follows:

...
...
BEGIN
    OPEN CURR FOR SELECT '"'
                         || PRODUCT_NAME
                         || '"|"'
                         || EXPIRY_DATE
                         || '"|"'
                         || OTHER_COLUMNS
                         || '"' AS CSV_LINES
                    FROM TEST_DATE
                   WHERE EXPIRY_DATE <= TO_DATE;

END;

Note: It is better practise to use the double quotes to wrap each the column values. We are trying to generate the csv with (pipe) delimiter, If there is a pipe in actual data then CSV will be corrupted if double quotes are not present. That is why double quotes are used.

© www.soinside.com 2019 - 2024. All rights reserved.