用于更新的 MSSQL 触发器仅工作一次
MSSQL Trigger for update is working only once
我要创建一个更新触发器。此触发器的目的是,如果 muadurum 列已更改,则在 table fkayitlar 中采用 mua_tarih 的旧值,并且插入另一个 table mua_tarihleri.
我的代码块像;
ALTER TRIGGER [dbo].[trgr_fkayit_update]
ON [dbo].[fkayitlar]
AFTER UPDATE
AS
DECLARE @mua_durum_once NVARCHAR(10)
DECLARE @mua_durum_sonra NVARCHAR(10)
DECLARE @mua_tarih_once DATE
DECLARE @mua_yapan_once NVARCHAR(25)
DECLARE @kisi_id INT
取旧值;
SELECT
@kisi_id=kayitid,
@mua_durum_once=muayenedurum,
@mua_tarih_once=muayenetarih,
@mua_yapan_once=mua_yapan
FROM deleted
取新值;
SELECT @mua_durum_sonra=muayenedurum FROM inserted
检查值是否改变;如果更改,插入 @mua_tarih 到 table mua_tarihleri 和 @kisi_id 和@mua_yapan_once
IF @mua_durum_once='OLDU'
AND @mua_durum_sonra='OLMADI'
AND @mua_tarih_once IS NOT NULL
BEGIN
INSERT INTO mua_tarihleri(kayitid,mua_tarihi,mua_yapan)
VALUES(@kisi_id,@mua_tarih_once,@mua_yapan_once)
END
我的问题是当我在 table fkayitlar 中更新多行时,触发器正在工作,但我在 table mua_tarih 中只看到插入的一行leri(只工作一次)。我需要查看不止一个。(应该不止一次工作)触发器不是在多个进程上工作吗? 或 我该如何解决这个问题?
触发器只会在 table 更新时发生一次,无论更新多少行。因此,您必须编写触发器主体以对一组行进行操作,而不是像您所做的那样对单个行进行操作。
应该是这样的:
ALTER TRIGGER [dbo].[trgr_fkayit_update]
ON [dbo].[fkayitlar]
AFTER UPDATE
AS
INSERT INTO mua_tarihleri(kayitid,mua_tarihi,mua_yapan)
SELECT deleted.kayitid, deleted.muayenedurum, deleted.muayenetarih, deleted.mua_yapan
FROM deleted
JOIN inserted ON deleted.kayitid = inserted.kayitid
WHERE deleted.muayenedurum='OLDU'
AND inserted.muayenedurum='OLMADI'
AND muayenetarih IS NOT NULL
我要创建一个更新触发器。此触发器的目的是,如果 muadurum 列已更改,则在 table fkayitlar 中采用 mua_tarih 的旧值,并且插入另一个 table mua_tarihleri.
我的代码块像;
ALTER TRIGGER [dbo].[trgr_fkayit_update]
ON [dbo].[fkayitlar]
AFTER UPDATE
AS
DECLARE @mua_durum_once NVARCHAR(10)
DECLARE @mua_durum_sonra NVARCHAR(10)
DECLARE @mua_tarih_once DATE
DECLARE @mua_yapan_once NVARCHAR(25)
DECLARE @kisi_id INT
取旧值;
SELECT
@kisi_id=kayitid,
@mua_durum_once=muayenedurum,
@mua_tarih_once=muayenetarih,
@mua_yapan_once=mua_yapan
FROM deleted
取新值;
SELECT @mua_durum_sonra=muayenedurum FROM inserted
检查值是否改变;如果更改,插入 @mua_tarih 到 table mua_tarihleri 和 @kisi_id 和@mua_yapan_once
IF @mua_durum_once='OLDU'
AND @mua_durum_sonra='OLMADI'
AND @mua_tarih_once IS NOT NULL
BEGIN
INSERT INTO mua_tarihleri(kayitid,mua_tarihi,mua_yapan)
VALUES(@kisi_id,@mua_tarih_once,@mua_yapan_once)
END
我的问题是当我在 table fkayitlar 中更新多行时,触发器正在工作,但我在 table mua_tarih 中只看到插入的一行leri(只工作一次)。我需要查看不止一个。(应该不止一次工作)触发器不是在多个进程上工作吗? 或 我该如何解决这个问题?
触发器只会在 table 更新时发生一次,无论更新多少行。因此,您必须编写触发器主体以对一组行进行操作,而不是像您所做的那样对单个行进行操作。
应该是这样的:
ALTER TRIGGER [dbo].[trgr_fkayit_update]
ON [dbo].[fkayitlar]
AFTER UPDATE
AS
INSERT INTO mua_tarihleri(kayitid,mua_tarihi,mua_yapan)
SELECT deleted.kayitid, deleted.muayenedurum, deleted.muayenetarih, deleted.mua_yapan
FROM deleted
JOIN inserted ON deleted.kayitid = inserted.kayitid
WHERE deleted.muayenedurum='OLDU'
AND inserted.muayenedurum='OLMADI'
AND muayenetarih IS NOT NULL