2016-02-24 69 views
0

我需要從my_view(即基於3個連接的表)複製或插入數據。在此之前,我已根據my_view創建了表my_table。 對於我使用以下步驟:Oracle過程將數據視圖複製到表

create or replace procedure view_copy(
      my_view in varchar2, 
      my_table in varchar2) 
is 
begin 
    execute immediate 'insert into '||my_table||' (select * from '||my_view||')'; 
end; 

的數據不會被複制到my_table。是否有可能將數據從視圖複製到表格?我怎樣才能以不同的方式編寫程序?

+0

當然這是允許的。但是如果你不指定列,那麼你的SQL可能會產生一個錯誤。 –

+0

物化視圖是否會工作而不是視圖?那麼你不需要從視圖複製到表格。 – MT0

回答

1

您的程序有效;如果您在目標表中找不到數據,也許您應該檢查一下您的視圖。 一個簡單的測試你的程序:

SQL> create table table1 (id1 number) 
    2/

Table created. 

SQL> create table table2 (id2 number) 
    2/

Table created. 

SQL> create or replace view v_t1_t2 as select id1, id2 from table1 cross join table2 
    2/

View created. 

SQL> create table table1_2 (id1 number, id2 number) 
    2/

Table created. 

SQL> create or replace procedure view_copy(
    2    my_view in varchar2, 
    3    my_table in varchar2) 
    4 is 
    5 begin 
    6  execute immediate 'insert into '||my_table||' (select * from '||my_view||')'; 
    7 end; 
    8/

Procedure created. 

SQL> insert into table1 values (1); 

1 row created. 

SQL> insert into table2 values (2); 

1 row created. 

SQL> exec view_copy('v_t1_t2', 'table1_2'); 

PL/SQL procedure successfully completed. 

SQL> select * from table1_2; 

     ID1  ID2 
---------- ---------- 
     1   2 
+0

是否有可能在沒有'立即執行'的情況下寫入過程 - 使用dbms_output.put_line查看數據是否已被插入? – m4ndy

+0

如果你需要真正執行'insert',你需要'立即執行'(或類似的解決方案);如果只想查看語句,可以使用'dbms_output',但這不會插入表中,只顯示語句 – Aleksej