0

我需要有关 SQL Server 语法的帮助来创建一些我在 SQLite 上成功使用过的触发器,但是一旦NEW.Table不是 SQL Server 的一部分,我就会遇到麻烦。

那是我的 SQLite 触发代码:

UPDATE EngineeringItems set ElevacaoFIT = CAST(round((select "Position Z" - MatchingPipeOD / 2 from EngineeringItems where PnPId = new.PNPID), 0) as INT) where PNPID = new.PNPID;

PNPID是表pk!

编辑2:

再次感谢朋友!我已经尝试了您的新代码并稍作修改,SQL Server 接受了它并成功创建了触发器。所以代码是:

CREATE TRIGGER [ElevacaoFIT_Insert]
   ON  [dbo].[EngineeringItems]
   AFTER INSERT
AS 
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    UPDATE EngineeringItems
SET
    ElevacaoFIT = CAST(ROUND((select EngineeringItems."Position Z" - (EngineeringItems.MatchingPipeOD / 2)
FROM
    EngineeringItems INNER JOIN inserted ON EngineeringItems.PnPId = inserted.PnPId), 0) AS INT);
END

但现在我收到这条消息:子查询返回超过 1 个值。当子查询跟随 =、!=、<、<=、>、>= 或子查询用作表达式时,这是不允许的。!任何想法?

抱歉打扰您了……再次感谢!!

4

1 回答 1

1

您可以发布您在 SQL Server 上尝试过的内容吗?

像下面这样的东西可能会这样做:

CREATE TRIGGER [dbo].[EngineeringItems_Trigger] 
   ON  [dbo].[EngineeringItems]
   AFTER INSERT
AS 
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

   UPDATE EngineeringItems
   SET
     ElevacaoFIT = CAST(round(("Position Z" - MatchingPipeOD / 2), 0) as INT)
   FROM
     EngineeringItems INNER JOIN inserted ON EngineeringItems.PnPId = inserted.PNPID;
END
于 2013-02-18T13:06:09.047 回答