这是我第一次处理 SQL Server 触发器。
在此之前,我为 MySql Server 编写了几乎相同的触发器,现在尝试为 SQL Server 重新编写代码。我修好了所有东西,但现在不明白它想要什么?我们还能如何访问刚刚插入的行的某些列,就像在 MySQL 中可能的那样 - NEW.some_field?
CREATE TRIGGER AntiCloneInsert ON dbo.user_item FOR INSERT AS
DECLARE @acc_id INT;
DECLARE @items_count INT;
IF (NEW.item_type in (select item_type from dbo.forbidden_item_types))
BEGIN
select @items_count = count(item_type) from dbo.user_item where item_type = NEW.item_type and warehouse = NEW.warehouse
IF (@items_count > 1)
BEGIN
select @acc_id = account_id from dbo.user_data where char_id = NEW.char_id
update lin2db.dbo.user_account set block_flag2 = 1 where uid = @acc_id
END
END
我尝试创建此触发器但出现此类错误:
Msg 4104, Level 16, State 1, Procedure AntiCloneInsert, Line 6
The multi-part identifier "NEW.item_type" could not be bound.Msg 4104, Level 16, State 1, Procedure AntiCloneInsert, Line 8
The multi-part identifier "NEW.item_type" could not be bound.Msg 4104, Level 16, State 1, Procedure AntiCloneInsert, Line 8
The multi-part identifier "NEW.warehouse" could not be bound.Msg 4104, Level 16, State 1, Procedure AntiCloneInsert, Line 11
The multi-part identifier "NEW.char_id" could not be bound.
最佳答案
SQL Server 没有new
和old
记录。相反,它有 inserted
和 deleted
表。这意味着可以使用集合操作(或循环,如果您真的愿意)来完成逻辑。
我认为下面是等价逻辑:
CREATE TRIGGER AntiCloneInsert ON dbo.user_item FOR INSERT AS
BEGIN
update ua
set block_flag2 = 1
from ua join
dbo.user_data ud
on ua.uid = ud.account_id join
inserted i
on ud.char_id = i.char_id join
dbo.forbidden_item_types it
on it.item_type = i.item_type
where (select count(it2.item_type)
from dbo.user_item it2
where it2.item_type = i.item_type and
it2.warehouse = i.warehouse
) > 1;
END
关于mysql - MySql 触发器中的 NEW.some_column 的 SQL Server 模拟是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33221333/