在创建使用plsql将数据加载到CSV文件的过程中需要帮助

时间:2018-07-03 08:19:50

标签: oracle plsql utl-file

我有三个表,如下所示。

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

表t2包含所有表名,每个源表和目标表的列名以及where子句条件。 [t2详细信息] [1]

在这里,我需要通过将事实表名称作为参数传递给分组,使用事实表,度量和where子句的列名将t3的数据插入到特定事实表中。

就像我们在过程中传递t1_Fact表一样,我们必须从t2中获取所有详细信息,并从t3中获取详细信息,然后插入t1_Fact中并将其保存到CSV文件中

我尝试了以下过程,但是无法将数据插入到csv文件中

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;

当我编译上面的过程时出现以下错误

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

请进一步帮助我。

谢谢。

1 个答案:

答案 0 :(得分:0)

  

PLS-00306:“ FOPEN”调用中参数的数量或类型错误

您的fopen调用仅传递了两个参数。您是将逗号连接到第一个值中,而不是将其用作参数分隔符,因此您将传递构造的字符串'<pathInfo>,<destinationTableName>.csv'作为位置,'W'作为文件名,而没有传递第三个参数打开模式。

如果我突出显示该问题,则可以看到:

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

继续使用连接空字符串(?)的模式,您会在右引号的反面:

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

甚至:

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

但是您并不需要做所有令人困惑的串联操作,只需执行以下操作即可:

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