当一行添加到客户表时,它应该:
在客户表中只有一个字段是电话号码。
此字段应复制到新的客户表中,并应从客户表中删除。
请有人告诉我这个触发器有什么问题.....
CREATE TRIGGER update_cus
ON customer
FOR update AS
IF (COLUMNS_UPDATED() & 1) > 0
BEGIN
INSERT INTO new_customer
(Phone number
)
SELECT 'NEW',
ins.Phone number
FROM inserted ins
SELECT 'OLD',
del.Phone number
FROM deleted del
END
答案 0 :(得分:2)
我的猜测
CREATE TRIGGER update_cus
ON customer
FOR update AS
IF EXISTS (SELECT * FROM INSERTED)
BEGIN
INSERT INTO new_customer ([Phone number])
SELECT 'NEW', ins.[Phone number]
FROM inserted ins
DELETE customer WHERE
[Phone number] IN (SELECT [Phone number] FROM deleted)
END
答案 1 :(得分:1)
您需要INSERT的行为,但已为UPDATE定义了触发器。
您的INSERT指定了一个字段,但您尝试插入2个值。
您声明要从客户删除记录,但没有DELETE语句。
我没有为你写一个不同的触发器,因为我实际上并不是100%肯定你想要实现的目标。
你能提供一些例子,包括各种表格在不同行动之前和之后的样子吗?
修改强>
CREATE TRIGGER update_cus
ON customer
FOR insert AS
BEGIN
INSERT INTO new_customer (name, id)
SELECT name, id
FROM inserted
DELETE customer WHERE
id IN (SELECT id FROM deleted)
-- This assumes id is Unique, but as the table should normally be empty, that's fine.
END