在创建程序方面需要帮助

时间:2018-06-28 13:03:56

标签: sql oracle plsql oracle-sqldeveloper plsqldeveloper

我有三个表,如下所示。

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 table data:

table t2 details

表t2包含所有表名,每个源表和目标表的列名以及where子句条件。

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

就像在过程中传递t1_Fact表一样,我们必须从t2获取所有详细信息,并从t3获取详细信息,然后插入t1_Fact。

我尝试了以下过程,但是无法将数据从t3插入事实表。

程序:

enter code here

**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.Table_Where_Clause%TYPE; 
sourceColumns t2.t3 columns%TYPE;
sourceMeasures t2.t3 measures%TYPE;
q1 VARCHAR2(3000 BYTE);
CURSOR C1 is 
    SELECT Table_Name, Table_Columns, Table_Measure, Table_Where_Clause 
--into reportName,procedureName,destinationTableName
 from t2 

BEGIN
 open c1;
 loop
   fetch c1 into tablename,destcolumns,destMeasures,whereClause;
   exit when c1%notfound;
    q1 := 'INSERT INTO ||tablename||"("||destColumns||","||destMeasures||","||Table_Name)"||
        " (select "||sourceColumns||","||sourceMeasures||","||sourceTableName||" FROM "||sourceTableName||" "||whereClause||
                              " GROUP BY "||sourceColumns||")';
   EXECUTE IMMEDIATE q1;


 end loop;
 close c1;

 End;

当我编译以上过程时,出现以下错误。

Error starting at line : 3 in command -
BEGIN CommonProcedure('MIS_CDR_01_01',1); END;
Error report -
ORA-00903: invalid table name
ORA-06512: at "AMTEL_MIS.PROCESSCDRCOMMONPROCEDURE", line 35
ORA-06512: at line 1
00903. 00000 -  "invalid table name"
*Cause:    
*Action:

我检查了表名和所有参数,得到了错误。

注意:上面的过程是我现在正在创建的一种类似过程的示例。

请进一步帮助我。

谢谢。

1 个答案:

答案 0 :(得分:1)

我想您要具备以下条件:

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


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

  Cursor c1 Is
  SELECT Table_Name, Table_Columns, Table_Measure, t3_Where_Clause
    FROM t2;

Begin
 Open c1;
 Loop
   Fetch c1 Into tablename,destcolumns,destMeasures,whereClause;
   Exit When c1%notfound;
    q1 := 'INSERT INTO '||tablename||'('||destColumns||','||destMeasures||','||tablename||')'||
        ' ( SELECT '||sourceColumns||','||sourceMeasures||','||sourceTableName
          ||' FROM '||sourceTableName||' '||whereClause||
           ' GROUP BY '||sourceColumns||')';

   Execute Immediate q1;
 End Loop;

 Close c1;

End;