更新触发器上的插入记录

时间:2015-05-14 11:36:59

标签: sql-server triggers insert sql-update

我在通过外部应用程序将记录插入数据库时​​尝试更新记录时遇到了麻烦。

我需要从应用程序中的用户插入的记录从表MinimumStock获取值AERO_LOCATIONSTOCKMIN,其中父级位置和资产类型

LOCATIONSTOCKMINID  ASSETTYPEID   LOCATIONID    MINIMUMSTOCK
   54000000001      54000000043   43200000357      12.00
   54000000002      54000000043   43200000883      6.00

这就是目的:当用户尝试在Asset表上插入记录(通过外部应用程序)时,触发器必须检查用户是否选择了Asset的位置。如果是,则触发器必须检索所选位置的父位置,并检查是否存在为此AssetType和此父位置定义的库存最小值。

如果完成所有操作,触发器必须将字段UDFText01设置为上表中定义的最小库存。

这是我迄今为止所尝试的内容。触发器正在检查要求,但在尝试更新并引发以下错误时失败:

The transaction ended in the trigger. The batch has been aborted

我尝试以多种方式修改UPDATE语句,但所有这些都会引发相同的错误消息。

修改

关注@Sean Lannge的建议我已经编辑了我的触发器以管理多个插件。触发器不再显示错误消息,但更改(插入)不会保存到数据库中。

ALTER TRIGGER [spectwosuite].[TRI_ASSET_STOCKMIN] ON [spectwosuite].[ASSET]
FOR INSERT AS 
BEGIN
  IF (UPPER(USER) != 'SPECTWOREPLENG')

    DECLARE @assettypeid numeric(15);
    DECLARE @assetid numeric(15);
    DECLARE @locationid numeric(15);
    DECLARE @parentlocationid numeric(15);
    DECLARE @stockmin numeric(9,2);
    DECLARE @mistock varchar(100);


    DECLARE crs_ROWS CURSOR FOR
    SELECT ASSETID, ASSETTYPEID, LOCATIONID
    FROM inserted;

    OPEN crs_ROWS;
      FETCH NEXT FROM crs_ROWS INTO @assetid, @assettypeid, @locationid 
      WHILE (@@FETCH_STATUS = 0)
      BEGIN

            SELECT @parentlocationID = ParentLocationID FROM Location 
                    LEFT JOIN Asset ON Asset.LocationID = Location.LocationID
                    WHERE Asset.LocationID = @locationid;                   


            IF NOT EXISTS 
                (SELECT * FROM Location LEFT JOIN INSERTED AS i ON Location.LocationID = i.LocationID 
                    WHERE Location.LocationID = i.LocationID)
                BEGIN

                    RAISERROR ('Please fill the Location for the Asset.',16,1);
                    ROLLBACK;
                END
            ELSE            

                    IF EXISTS (SELECT MinimumStock FROM AERO_LOCATIONSTOCKMIN
                        WHERE AssetTypeID = @assettypeid AND LocationID = (SELECT ParentLocationID FROM Location WHERE LocationID = @locationID))

                        BEGIN

                            SELECT @stockmin = MinimumStock FROM AERO_LOCATIONSTOCKMIN
                                WHERE AssetTypeID = @assetTypeID AND LocationID = 
                                        (SELECT ParentLocationID FROM Location WHERE LocationID = @locationID);

                                SELECT @mistock= CONVERT(varchar(100),@stockmin);   
                                --RAISERROR (@mistock,16,1);            


                                UPDATE spectwosuite.ASSET
                                SET UDFText01 = @mistock 
                                FROM
                                     INSERTED I
                                INNER JOIN spectwosuite.ASSET T ON
                                     T.AssetID = I.AssetID

                            --UPDATE spectwosuite.ASSET SET UDFText01 = @mistock
                            --  FROM spectwosuite.ASSET AS A INNER JOIN inserted AS I
                            --  ON A.AssetID = I.AssetID;                   

                        END

                    ELSE
                            RAISERROR ('The parent Location for the Asset Type doesn't have minimum stock defined',16,1);
                            ROLLBACK;

       FETCH NEXT FROM crs_ROWS INTO @assetid, @assettypeid, @locationid; 
      END;
      CLOSE crs_ROWS;
      DEALLOCATE crs_ROWS;

END;

1 个答案:

答案 0 :(得分:1)

根据此处的扩展讨论,看看这样的事情是否与你想要做的事情更接近。

if(chmod!=ch){
        chLast=chmod;
        i++;
          if(i==1)
        chFirst=chmod;
    }