0

我有一个名为的表tbl_gallery,其中有一列bit名为isActive.

当用户更新该IsActive值时,其他行将IsActive = true自动变为 false。

如何使用更新的触发器来做到这一点?

请帮忙

4

2 回答 2

1

我想你想要这样的东西:

CREATE TRIGGER trgGalleryActive
ON dbo.tbl_gallery
FOR UPDATE
AS
    BEGIN
        UPDATE g
        -- Update all other gallery rows for this same user to false
            SET g.IsActive = 0
            FROM tbl_gallery g
                INNER JOIN inserted i
                on g.UserPK = i.UserPK
            WHERE
                -- However, we don't want current inserted records to be updated
                g.TablePK <> i.TablePK
                            -- As per Marc's comment - don't update existing inactive rows unnecessarily
                            AND g.IsActive = 1
                            -- Only if this record is active should any of this happen
                            AND i.IsActive = 1
    END
于 2012-04-17T05:18:38.737 回答
0

更新第一个表后触发更新第二个表:

CREATE TRIGGER update_table_cityUpdated_afterTable_cityUpdate
  ON Table_city 
  AFTER UPDATE AS
BEGIN
  DECLARE @cityId AS BIGINT
  DECLARE @stateId AS BIGINT
  DECLARE @CityName AS NVARCHAR(200)

  SELECT @cityId=cityId FROM INSERTED
  SELECT @stateId= stateId FROM INSERTED
  SELECT @CityName= CityName FROM INSERTED
  UPDATE table_cityUpdated
 SET
    [dbo].[table_cityUpdated].stateId=@stateId,
    [dbo].[table_cityUpdated].CityName=@CityName
  WHERE [dbo].[table_cityUpdated].cityId=@cityId
END
;
于 2015-02-06T22:03:44.423 回答