从触发器中捕获插入的ID

时间:2016-01-07 12:39:54

标签: c# sql sql-server triggers

我是SQL的新手,所以不要叫我。我有一个触发器的问题,它意味着捕获最后插入的数据并更新它。

create trigger ilgKodIsim on URTFIS instead of insert
as
SET NOCOUNT ON
SET IDENTITY_INSERT URTFIS ON

declare @id int;
insert into URTFIS ( [ID], [FISNO], [FISTAR], [URTTAR], [STATUS], [STOKID], [DEPOID], [BIRIMID], [MIKTAR],
[BIRIM1KATSAYI], [BIRIM2KATSAYI], [BIRIM3KATSAYI], [FIREORAN], [SIPARISID], [PROJEID], [KUR_RAPOR], 
[GENELTOPLAM], [GENELTOPLAM_RAPOR], [OZELGRUP1], [OZELGRUP2], [OZELALAN1], [OZELALAN2], [OZELALAN3], [OZELALAN4], [OZELALAN5], 
[OZELALAN6], [OZELALAN7], [OZELALAN8], [OZELALAN9], [OZELALAN10], [OZELALAN11], [OZELALAN12], [OZELALAN13],
[OZELALAN14], [OZELALAN15], [OZELALAN16], [OZELALAN17], [OZELALAN18], [OZELALAN19], [OZELALAN20], 
[ACIKLAMA], [BARKODID], [FATFISID], [MUHFISID], [SUBEID], [YETKIKODID], [OWN_URTFISID], [MASTER_URTFISID], 
[OTOMATIK_ALT_URUN], [GENELGRUP1], [GENELGRUP2], [GENELGRUP3], [GENELGRUP4], [GENELGRUP5], [ERP_LOTID], 
[ERP_LOT_CIKIS_GIRIS_HARID], [ERP_VARYANTID], [STKFISID], [STKFISHARID])

select

[ID], [FISNO], [FISTAR], [URTTAR], [STATUS], [STOKID], [DEPOID], [BIRIMID], [MIKTAR],
[BIRIM1KATSAYI], [BIRIM2KATSAYI], [BIRIM3KATSAYI], [FIREORAN], [SIPARISID], [PROJEID], [KUR_RAPOR], 
[GENELTOPLAM], [GENELTOPLAM_RAPOR], [OZELGRUP1], [OZELGRUP2],[OZELALAN1], [OZELALAN2], [OZELALAN3], [OZELALAN4], [OZELALAN5], 
[OZELALAN6], [OZELALAN7], [OZELALAN8], [OZELALAN9], [OZELALAN10], [OZELALAN11], [OZELALAN12], [OZELALAN13],
[OZELALAN14], [OZELALAN15], [OZELALAN16], [OZELALAN17], [OZELALAN18], [OZELALAN19], [OZELALAN20], 
[ACIKLAMA], [BARKODID], [FATFISID], [MUHFISID], [SUBEID], [YETKIKODID], [OWN_URTFISID], [MASTER_URTFISID], 
[OTOMATIK_ALT_URUN], [GENELGRUP1], [GENELGRUP2], [GENELGRUP3], [GENELGRUP4], [GENELGRUP5], [ERP_LOTID], 
[ERP_LOT_CIKIS_GIRIS_HARID], [ERP_VARYANTID], [STKFISID], [STKFISHARID]

from inserted

SET @id = Scope_Identity()

update URTFIS set OZELALAN2 = 'Deneme' where ID = @id

SET IDENTITY_INSERT URTFIS OFF

检查后,值未更新。我无法弄明白。

2 个答案:

答案 0 :(得分:0)

由于您使用的是URTFIS.ID,因此您无法获得ID的新身份值。

只需使用inserted中的update URTFIS set OZELALAN2 = 'Deneme' where ID = SELECT TOP 1 Id FROM Inserted

create trigger ilgKodIsim on URTFIS instead of insert
as
SET NOCOUNT ON


declare @id int;
insert into URTFIS ([FISNO], [FISTAR], [URTTAR], [STATUS], [STOKID],    [DEPOID], [BIRIMID], [MIKTAR],
[BIRIM1KATSAYI], [BIRIM2KATSAYI], [BIRIM3KATSAYI], [FIREORAN], [SIPARISID], [PROJEID], [KUR_RAPOR], 
[GENELTOPLAM], [GENELTOPLAM_RAPOR], [OZELGRUP1], [OZELGRUP2], [OZELALAN1], [OZELALAN2], [OZELALAN3], [OZELALAN4], [OZELALAN5], 
[OZELALAN6], [OZELALAN7], [OZELALAN8], [OZELALAN9], [OZELALAN10],     [OZELALAN11], [OZELALAN12], [OZELALAN13],
[OZELALAN14], [OZELALAN15], [OZELALAN16], [OZELALAN17], [OZELALAN18],     [OZELALAN19], [OZELALAN20], 
[ACIKLAMA], [BARKODID], [FATFISID], [MUHFISID], [SUBEID], [YETKIKODID], [OWN_URTFISID], [MASTER_URTFISID], 
[OTOMATIK_ALT_URUN], [GENELGRUP1], [GENELGRUP2], [GENELGRUP3], [GENELGRUP4], [GENELGRUP5], [ERP_LOTID], 
[ERP_LOT_CIKIS_GIRIS_HARID], [ERP_VARYANTID], [STKFISID], [STKFISHARID])

select

[FISNO], [FISTAR], [URTTAR], [STATUS], [STOKID], [DEPOID], [BIRIMID], [MIKTAR],
[BIRIM1KATSAYI], [BIRIM2KATSAYI], [BIRIM3KATSAYI], [FIREORAN], [SIPARISID], [PROJEID], [KUR_RAPOR], 
[GENELTOPLAM], [GENELTOPLAM_RAPOR], [OZELGRUP1], [OZELGRUP2],[OZELALAN1], [OZELALAN2], [OZELALAN3], [OZELALAN4], [OZELALAN5], 
[OZELALAN6], [OZELALAN7], [OZELALAN8], [OZELALAN9], [OZELALAN10], [OZELALAN11], [OZELALAN12], [OZELALAN13],
[OZELALAN14], [OZELALAN15], [OZELALAN16], [OZELALAN17], [OZELALAN18], [OZELALAN19], [OZELALAN20], 
[ACIKLAMA], [BARKODID], [FATFISID], [MUHFISID], [SUBEID], [YETKIKODID], [OWN_URTFISID], [MASTER_URTFISID], 
[OTOMATIK_ALT_URUN], [GENELGRUP1], [GENELGRUP2], [GENELGRUP3], [GENELGRUP4],     [GENELGRUP5], [ERP_LOTID], 
[ERP_LOT_CIKIS_GIRIS_HARID], [ERP_VARYANTID], [STKFISID], [STKFISHARID]

from inserted

SET @id = Scope_Identity()

update URTFIS set OZELALAN2 = 'Deneme' where ID = @id

或者不要明确插入ID:

createMovie.create({
  release_date:   releaseNL.release_date,
  imdb_rating:    $scope.movieImdbRating.imdbRating,
  title:          $scope.movieListID.original_title,
  image:          $scope.movieListID.poster_path,
  movie_id:       $scope.movieListID.id,
  backdrop:       $scope.movieListID.backdrop_path,
  crew:           $scope.movieCrew = response.credits.crew,
  cast:           $scope.movieCast = response.credits.cast
}).then(init);

答案 1 :(得分:0)

我会猜测你所需要的实际上更多的是:

create trigger ilgKodIsim on URTFIS
after insert --not instead of
as
SET NOCOUNT ON

update URTFIS set OZELALAN2 = 'Deneme' where ID IN (select ID from inserted)

我认为没有理由编写instead of触发器,其中第一个动作是执行完全相同的插入,如果没有触发器就会发生。

试图把"""插入标识列的值到标量变量中注定要失败,因为它可以编写多行INSERT语句(实际上,原始触发器中有一个例子)。触发器只触发一次,inserted中有多个行,因此多个标识值刚刚被分配。