我有两张桌子。
我想同时在两张桌子上插入相同的记录 即,当我为第一个表插入记录时,同样的记录也会使用触发器插入到第二个表中。
您是否在此过程中有任何经验/建议?
答案 0 :(得分:4)
如果您正在使用存储过程,则可以轻松管理此
CREATE PROCEDURE sp_Insert
@Value varchar(10)
AS
insert into table1 (...,...) values (@value,...)
insert into table2 (...,...) values (@value,...)
答案 1 :(得分:1)
我建议在触发器上使用Erik的方法。触发器往往会导致性能问题,很多时候,您会忘记触发器存在,并获得意外行为。但是,如果您确实想要使用触发器,它将起作用。这是一个例子:
CREATE TRIGGER trgTest ON Test
FOR INSERT
AS
INSERT Test2
(Id, value)
SELECT Id, Value
FROM Inserted
答案 2 :(得分:0)
可以使用光标概念!
CREATE OR REPLACE TRIGGER bi_order
BEFORE INSERT
ON ord
REFERENCING OLD AS OLD NEW AS NEW
FOR EACH ROW
WHEN (NEW.payment_type = 'CREDIT')
DECLARE
CURSOR cur_check_customer IS
SELECT 'x'
FROM customer
WHERE customer_id = :NEW.customer_id
AND credit_rating = 'POOR';
lv_temp_txt VARCHAR2(1);
lv_poor_credit_excep EXCEPTION;
BEGIN
OPEN cur_check_customer;
FETCH cur_check_customer INTO lv_temp_txt;
IF (cur_check_customer%FOUND) THEN
CLOSE cur_check_customer;
RAISE lv_poor_credit_excep;
ELSE
CLOSE cur_check_customer;
END IF;
EXCEPTION
WHEN lv_poor_credit_excep THEN
RAISE_APPLICATION_ERROR(-20111, 'Cannot process CREDIT ' ||
'order for a customer with a POOR credit rating.');
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20122, 'Unhandled error occurred in' ||
' BI_ORDER trigger for order#:' || TO_CHAR(:NEW.ORDER_ID));
END bi_order;