mysql - MySql 触发器中的 NEW.some_column 的 SQL Server 模拟是什么?

标签 mysql sql sql-server

这是我第一次处理 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 没有newold 记录。相反,它有 inserteddeleted 。这意味着可以使用集合操作(​​或循环,如果您真的愿意)来完成逻辑。

我认为下面是等价逻辑:

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/

相关文章:

jquery - 如何使用简单的 html 表单字段而不使用 ASP、JSP 或 PHP 连接数据库

SQL Server 2014 : INSERT INTO values & subqueries

sql - 将 varchar 值转换为 smallint 数据类型时转换失败

sql-server - 存储过程中的经典 ADO 和表值参数

mysql - 没有这样的列 : admin_users. 密码

mysql - 如何在Access表单中以多对多关系填充第三个表? (MySQL后端)

sql - 如果存在,如何在 MySQL 中更新,如果不存在则插入(AKA "upsert"或 "merge")?

java - 如何添加 JAVA 时间戳以使 mySQL 工作?

sql - 从表示树结构的 Oracle 表中选择所有后代行

sql-server - SQL Server 获取唯一行或具有最大字段值的行