在所有表中创建视图和更改

时间:2016-05-16 22:15:55

标签: sql oracle plsql dynamic-sql

我正在尝试使用游标在过程中创建逻辑,以便为所有表创建数据库视图,并为模式中的所有表创建一个名为create or replace PROCEDURE ALTER_TABLES( RC OUT INT ,MSG OUT VARCHAR) AS BEGIN DECLARE CURSOR TBL_CUR IS SELECT TABLE_NAME FROM USER_TABLES; TBL_REC TBL_CUR%ROWTYPE; SQL_STMT VARCHAR(2000); BEGIN OPEN TBL_CUR; LOOP FETCH TBL_CUR INTO TBL_REC; EXIT WHEN TBL_CUR%NOTFOUND; PRINT_DETAILS(TBL_REC.TABLE_NAME); SQL_STMT:= 'ALTER TABLE ' || TBL_REC.TABLE_NAME || ' ADD HISTORY_DATE DATE' || ' AND' || ' CREATE OR REPLACE VIEW all_tbl AS' || ' SELECT *' || ' FROM USER_TABLES' ; PRINT_DETAILS(SQL_STMT); EXECUTE IMMEDIATE SQL_STMT; END LOOP; CLOSE TBL_CUR; rollback; END; END; 的新列。我需要帮助构建下面的逻辑。

import java.io.Serializable;

public class Menu_Items implements Serializable {

   private String Name = "";
   private String Description = "";
   private float Price = 0.0f;
   private String Category = "";

    /* Getters and Setters*/    
}

1 个答案:

答案 0 :(得分:0)

您只想创建一次视图;戈登·林诺夫(Gordon Linoff)提到,你不能同时做两件事。所以在循环之外创建视图;这样的事情(未经测试):

create or replace PROCEDURE ALTER_TABLES(RC OUT INT
                                    ,MSG OUT VARCHAR)
AS
BEGIN

DECLARE

CURSOR TBL_CUR IS
SELECT TABLE_NAME FROM USER_TABLES;
TBL_REC TBL_CUR%ROWTYPE;  
SQL_STMT  VARCHAR(2000);

BEGIN
          SQL_STMT  := 'CREATE OR REPLACE VIEW all_tbl AS'
                 || ' SELECT *'
                 || ' FROM USER_TABLES';
          PRINT_DETAILS(SQL_STMT);
          EXECUTE IMMEDIATE SQL_STMT;

          OPEN TBL_CUR;
              LOOP
                FETCH TBL_CUR INTO TBL_REC;
                EXIT WHEN TBL_CUR%NOTFOUND;

                PRINT_DETAILS(TBL_REC.TABLE_NAME);            


                 SQL_STMT:= 'ALTER TABLE '
                 || TBL_REC.TABLE_NAME
                 || ' ADD (HISTORY_DATE DATE)'
                 ;

                  PRINT_DETAILS(SQL_STMT);
                  EXECUTE IMMEDIATE SQL_STMT;


              END LOOP;
          CLOSE TBL_CUR;

--rollback;

END;

现在,我认为无需创建视图all_tbl,只需查看视图的副本USER_TABLES - 所以只需使用USER_TABLES - - 但是我把它留在了那里,所以如果你只需要来自USER_TABLES或某些行的某些列,你知道在哪里放置它。