2016-05-30 44 views
1

我有一個名爲PKCHANGES的表,它有幾列,其中之一是primary_key列。我想要的是在其他表上創建一個觸發器,並且在插入時我抓取一些值並將它們發佈到PKCHANGES表。除了當我嘗試發佈主鍵值時,一切都很好。我希望在primary_key列中輸入主鍵值逗號分隔。所以如果TableX有3個主鍵,在PKCHANGES(primary_key列)中,我發佈了value1,value2,value3。動態查詢:新值

到目前爲止,我只設法得到以下的結果,而不是實際值 「:new.pkCol1:new.pkCol2:new.pkCol3」

我的PL/SQL塊是:

DECLARE 
mySql varchar2(5000); 
myTable varchar2(10) := 'TableX'; 
BEGIN 
    mySql := 'CREATE OR REPLACE TRIGGER ' || 't_1' || ' AFTER INSERT ON ' || myTable || ' 
     FOR EACH ROW 
     DECLARE 
     currentPK varchar2(200); --Contains the current primary key value in the loop 
     result varchar2(200); --Contains the appended string of primary key values 

     --Cursor that contains primaryKeys for table 
     CURSOR pks IS 
     SELECT cols.column_name FROM all_constraints cons, all_cons_columns cols 
     WHERE cons.constraint_type = ''P'' 
     AND cons.constraint_name = cols.constraint_name 
     AND cons.table_name = ' || '''' || myTable || '''' || '; 

     BEGIN 
     --Loop through primary keys, get the value from the trigger, and append the string. 
     for current_pk IN pks LOOP 
      BEGIN 
      currentPK := '':new.'' || current_pk.column_name; 
      result:= result || currentPK; 
     END; 
    END LOOP;' 
    || 
    ' --Insert the appended values into the primary_key column 
     INSERT INTO PKCHANGES(primary_key)' || 
      'VALUES (result);' 
    || ' END;'; 
    dbms_output.put_line(mySql); 
EXECUTE IMMEDIATE mySql; 
END; 

任何想法?

回答

2

不需要在每個插入查詢TableX的主鍵。它是穩定的,如果一旦改變,你也會改變觸發器。

這允許您彈出觸發器以外的邏輯。

在第一步中連接PK。我更喜歡LISTAGG,因爲它優雅的分界符。你得到類似:new.COL1||','||:new.COL2||','||:new.COL3

還要確保表名是正確的(我假設大寫;否則你需要引用名稱)。

在接下來的步驟產生觸發,將basicaly只包含INSERT

DECLARE 
mySql varchar2(5000); 
myTable varchar2(10) := 'TableX'; 
result varchar2(200); -- Contains the concatenated string of primary key column names with delimiters, 
         -- e.g. ":new.COL1||','||:new.COL2||','||:new.COL3" 
BEGIN 
     SELECT listagg(':new.'||cols.column_name,'||'',''||') within group (order by position) into result 
     FROM all_constraints cons, all_cons_columns cols 
     WHERE cons.constraint_type = 'P' 
     AND cons.constraint_name = cols.constraint_name 
     AND cons.table_name = upper(myTable); 

    mySql := 'CREATE OR REPLACE TRIGGER ' || 't_1' || ' AFTER INSERT ON ' || myTable || ' 
     FOR EACH ROW 
     BEGIN 
     --Insert the appended values into the primary_key column 
     INSERT INTO PKCHANGES(primary_key)' || 
      'VALUES ('||result||');'  
    || ' END;'; 
    dbms_output.put_line(mySql); 
EXECUTE IMMEDIATE mySql; 
END; 
/

測試

create table TableX 
(col1 number, 
col2 number, 
col3 number, 
col4 number); 
alter table TableX add (primary key (col1, col2, col3)); 

insert into TableX values (1,2,3,4); 


select * from PKCHANGES; 

PRIMARY_KEY 
----------- 
1,2,3 
+0

輝煌謝謝:) – Dragonfly