Sql 删除后触发器未插入多行
看Sql 删除后触发器未插入多行,sql,sql-server,Sql,Sql Server,看 DELETED可以包含多行,因此请改用SELECT 就你而言: CREATE TRIGGER TRIG_SomeTable_Delete ON SomeTable AFTER DELETE AS BEGIN INSERT INTO SomeTable(SomeColumn) SELECT SomeColumn FROM DELETED END 可以包含多行。因此,将列中的值指定给标量变量总是一个
DELETED
可以包含多行,因此请改用SELECT
就你而言:
CREATE TRIGGER TRIG_SomeTable_Delete
ON SomeTable AFTER DELETE
AS
BEGIN
INSERT INTO SomeTable(SomeColumn)
SELECT SomeColumn FROM DELETED
END
可以包含多行。因此,将列中的值指定给标量变量总是一个错误
请尝试:
ALTER TRIGGER [tgr_del_into_plan_audit]
ON [dbo].[agent_plan_details]
AFTER DELETE
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO agent_plan_details_audit
(
plan_title_id,
plan_title,
no_of_property_listing,
validity_of_plan,
Each_property_listing_life,
list_of_property_in_search_result,
price,
discount,
discount_code,
discount_percentage,
create_date,
action
)
SELECT
plan_title_id,
plan_title,
no_of_property_listing,
validity_of_plan,
Each_property_listing_life,
list_of_property_in_search_result,
price,
discount,
discount_code,
discount_percentage,
GETDATE(),
'D'
FROM DELETED
END
有人一定会指出一些边缘情况,在触发器中使用这样的语句不是错误:
ALTER
TRIGGER [tgr_del_into_plan_audit]
ON [dbo].[agent_plan_details]
AFTER DELETE
AS
SET NOCOUNT ON
INSERT INTO agent_plan_details_audit
( plan_title_id,
plan_title,
no_of_property_listing,
validity_of_plan,
Each_property_listing_life,
list_of_property_in_search_result,
price,
discount,
discount_code,
discount_percentage,
create_date,
action )
SELECT
plan_title_id,
plan_title,
no_of_property_listing,
validity_of_plan,
Each_property_listing_life,
list_of_property_in_search_result,
price,
discount,
discount_code,
discount_percentage,
GETDATE(),
'D'
FROM
deleted
但是这样的情况是少数——我想不出有什么好的
您当前查询中的每个
SELECT
s都将从deleted
的任意行中选择一个值-甚至不能保证每个SELECT
s都将从同一任意行中获得一个值。这是用于什么数据库系统的?触发器没有标准化,sql
标记用于标准sql语言。另外,既然您已经编写了一个部分工作的触发器,那么代码在哪里?向我们展示您的触发器。:)如果你发布你当前的代码,我们也许可以修复它。什么样的行?网格行?HTML表格行?SQL行?@jlafaySQL
已标记。;)谢谢你,先生。它起作用了。
ALTER
TRIGGER [tgr_del_into_plan_audit]
ON [dbo].[agent_plan_details]
AFTER DELETE
AS
SET NOCOUNT ON
INSERT INTO agent_plan_details_audit
( plan_title_id,
plan_title,
no_of_property_listing,
validity_of_plan,
Each_property_listing_life,
list_of_property_in_search_result,
price,
discount,
discount_code,
discount_percentage,
create_date,
action )
SELECT
plan_title_id,
plan_title,
no_of_property_listing,
validity_of_plan,
Each_property_listing_life,
list_of_property_in_search_result,
price,
discount,
discount_code,
discount_percentage,
GETDATE(),
'D'
FROM
deleted
select @plan_title_id=d.plan_title_id from deleted d;