将值复制到新表并从旧表中删除值

时间:2011-06-02 06:01:42

标签: mysql triggers

CREATE TRIGGER trigg_msg 
ON messagein 

FOR insert,delete,update AS 
IF EXISTS (SELECT * FROM INSERTED) 
BEGIN 

INSERT INTO new_message (Phone number,CusID,CusName,CusAddress) values ( SELECT ins.sender FROM inserted ins, SELECT cus.CusID FROM customer cus, SELECT cusname FROM customer cus, SELECT cus.cus_phone number FROM customer cus, SELECT cus.Address FROM customer cus) 
DELETE * FROM messagein 
END 

这个触发器不起作用。请有人告诉我错误....

当在messagein表中插入一行时,它应该复制sender字段的值并将其复制到new_message表。然后应该从customer表中检索其他值,如cusID,CusName,..请有人帮助我。

更新

CREATE TRIGGER trigg_msg 

ON messagein 

FOR insert

IF EXISTS (SELECT * FROM INSERTED)

BEGIN

INSERT INTO new_message(Phone_number,CusID,CusName,CusAddress) values ( SELECT ins.sender FROM inserted ins, SELECT cus.CusID, cus.cusname, cus.cus_phone_number,  cus.Address FROM customer cus)

DELETE FROM messagein 

@Devart和@TudorConstantin非常感谢你的帮助。最后我想出了这个触发器,但仍然出现错误。

1 个答案:

答案 0 :(得分:1)

您至少有一个语法错误:

DELETE * FROM messagein 

应该是:

DELETE FROM messagein 

此外,INSERT应为:

INSERT INTO new_message (Phone_number,CusID,CusName,CusAddress) values ( SELECT ins.sender FROM inserted ins, SELECT cus.CusID, cus.cusname, cus.cus_phone_number,  cus.Address FROM customer cus)