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子句条件。
在这里,我需要通过将事实表名称作为参数传递给分组,使用事实表,度量和where子句的列名将t3的数据插入到特定事实表中。
就像我们在过程中传递t1_Fact表一样,我们必须从t2中获取所有详细信息,并从t3中获取详细信息,然后插入t1_Fact中并将其保存到CSV文件中
我尝试了以下步骤,但是无法将数据插入到csv文件中。
注意:在以下过程中,我需要将表(表名)数据提取到q2中,我已将变量声明为recordsFetched VARCHAR2(3000 BYTE);
。
需要知道我们如何将记录声明为%rowtype
的recordsFetched,而从光标获取表名时却不知道表名。
我还能在UTL_FILE.PUT_Line()
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);
q2 VARCHAR2(3000 BYTE);
recordsFetched VARCHAR2(3000 BYTE);
pathInfo VARCHAR2(3000 BYTE);
v_file UTL_FILE.FILE_TYPE;
Cursor TableName Is SELECT Table_Name FROM t2;
Begin
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;
q2 := 'SELECT ' || destColumns || ',' || destMeasures ||' FROM ' || tablename || '';
dbms_output.put_line(q2);
EXECUTE IMMEDIATE q2 into recordsfetched; --Getting an error here
v_file := UTL_FILE.FOPEN('USER_DIR', destinationTableName ||'.csv', 'W');
UTL_FILE.PUT_Line(v_file, recordsfetched);
UTL_FILE.FCLOSE(v_file);
End Loop;
Close TableName;
End;
当我执行上述过程时出现以下错误
Error report -
ORA-00932: inconsistent datatypes: expected - got -
ORA-06512: at "GENERATECSV", line 47
ORA-06512: at line 1
00932. 00000 - "inconsistent datatypes: expected %s got %s"
*Cause:
*Action:
请进一步帮助我。
谢谢。
答案 0 :(得分:0)
您正在将两个值(destColumns和destMeasures)选择到一个变量(recordsfetched)中。
rabbitmqctl eval 'Q = {resource, <<"/">>, queue, <<"sharding: sharded_queue - rabbit@hostname - 0">>}, rabbit_amqqueue:internal_delete(Q).'
应该是这样的:
q2 := 'SELECT ' || destColumns || ',' || destMeasures ||' FROM ' || tablename || '';
EXECUTE IMMEDIATE q2 into recordsfetched;
答案 1 :(得分:0)
这是一个复杂的过程。但是a不解。为了使第二个动态查询有效,您需要选择一个与组合查询的投影相匹配的记录集。因此,您需要生成一些动态代码来做到这一点。
或者您可以尝试以下操作:
....
Execute Immediate q1;
destColumns := replace( destColumns, ',', q'[||','||]');
destMeasures := replace( destMeasures, ',', q'[||','||]');
q2 := 'SELECT ' || destColumns || ',' || destMeasures ||' FROM ' || tablename || '';
dbms_output.put_line(q2);
EXECUTE IMMEDIATE q2 into recordsfetched;
....
这会将投影变成单个宽列,可以将其写入文件。