简体   繁体   中英

Need assistance in creating a procedure to load the data into CSV file using plsql

I have a three tables as below.

Create table t1_Fact ( Cur_date Date, Name varchar2(10), Event varchar2(50), Price Number(10,0), TAX Number(10,0), Flag Number );
Create table App_Fact ( Application_ID Number, Application_Name varchar2(100), Application_Price Number, Appliation_Tax Number, Flag Number );
Create table t2 ( Table_Name Varchar2(100), Table_Columns Varchar(100), Table_Measure varchar2(100), t3_columns varchar2(100), t3_measures varchar2(100), t3_Where_Clause varchar2(100) );
Create table t3 ( Cur_date Date, Name varchar2(10), Event varchar2(50), Application_ID Number, Application_Name varchar2(100), Application_Price Number, Appliation_Tax Number, Price Number(10,0), TAX Number(10,0), Flag Number );

table t2 contains all the table names,column names of each source and destination tables and where clause conditions. [t2 Details][1]

Here I need to insert the data from t3 to particular fact table by using group by the column names of fact table, measures and where clause by passing the fact table name as parameter.

Like if we pass t1_Fact table in procedure, we must get all the details from t2 and get the details from t3 and insert into t1_Fact and also save them into CSV file

I have tried the following procedure however I'm not able to insert the data into csv file

Create or Replace Procedure CommonProcedure(sourceTableName IN VARCHAR2) Is
  tablename t2.Table_Name%TYPE;
  destcolumns t2.Table_Columns%TYPE;
  destMeasures t2.Table_Measure%TYPE;
  whereClause t2.t3_Where_Clause%TYPE;
  sourceColumns t2.t3_columns%TYPE;
  sourceMeasures t2.t3_measures%TYPE;
  q1 VARCHAR2(3000 BYTE);
  pathInfo VARCHAR2(3000 BYTE);

   v_file  UTL_FILE.FILE_TYPE;

 Cursor TableName Is  SELECT Table_Name FROM t2;

Begin

--Path will be getting from another table using the function in the format of '/data/Oracle-files/Table_CSV'

pathInfo := getDBConfigParamValue('FILE_LOCATION');

 Open c1;
 Loop
   Fetch TableName Into tablename;
   Exit When TableName%notfound;

    SELECT Table_Columns, Table_Measure, t3_columns, t3_measures INTO destcolumns,destMeasures,sourceColumns,sourceMeasures FROM t2 where Table_Name = tablename;

    q1 := 'INSERT INTO '||tablename||'('||destColumns||','||destMeasures||')'||
        ' ( SELECT '||sourceColumns||','||sourceMeasures||','||sourceTableName
          ||' FROM '||sourceTableName||' GROUP BY '||sourceColumns||')';

    Execute Immediate q1;



    --Need to load the data into  tablename.CSV

    v_file := UTL_FILE.FOPEN('' || pathInfo ||',' ||destinationTableName ||'.csv' || '','W');

    UTL_FILE.PUT_LINE(v_file,'' ||destColumns ||',' ||destMeasures ||'');

    UTL_FILE.FCLOSE(v_file);

 End Loop;

 Close TableName;

End;

When I compile the above procedure getting following error

LINE/COL ERROR
-------- -----------------------------------------------------------------
47/13    PL/SQL: Statement ignored
47/23    PLS-00306: wrong number or types of arguments in call to 'FOPEN'

Please assist me further.

Thanks in advance.

PLS-00306: wrong number or types of arguments in call to 'FOPEN'

Your fopen call is only passing two arguments; you are concatenating a comma into the first value, rather than using it as an argument separator, so you are passing the constructed string '<pathInfo>,<destinationTableName>.csv' as the location, 'W' as the filename, and no third argument for the open mode.

You can see the problem if I highlight it:

v_file := UTL_FILE.FOPEN('' || pathInfo ||',' ||destinationTableName ||'.csv' || '','W');
                                        ^^^^^^^^

Continuing your pattern of concatenating empty strings (?) you have it the wrong side of a closing quote:

v_file := UTL_FILE.FOPEN('' || pathInfo ||'', destinationTableName ||'.csv' || '','W');

or maybe even:

v_file := UTL_FILE.FOPEN('' || pathInfo ||'', '' || destinationTableName ||'.csv' || '','W');

but you don't need to do all that confusing concatenation, you can just do:

v_file := UTL_FILE.FOPEN(pathInfo, destinationTableName ||'.csv', 'W');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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