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;
任何想法?
輝煌謝謝:) – Dragonfly