我有一个触发器,但是我需要获取更新记录的主键(比如插入数据 SELECT @Id= @@IDENTITY),因此我可以将它传递给 where 条件。我怎样才能做到这一点?
ALTER TRIGGER [dbo].[CariBakiyeBorcAktar]
ON [dbo].[BakimKartiDegisenParcalar]
AFTER UPDATE
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @Id int
DECLARE @CariId int
DECLARE @SId int
DECLARE @MId int
declare @Tutar decimal
declare @Bakiye decimal
declare @s decimal = 0
DECLARE @ParcaId int
--how I can I get the last updateed record Identity like this??
--and pass it to update query as a where condition
SELECT @Id= @@IDENTITY
set @SId=(select SId from CariBakiye where Id =@Id)
select @CariId=tblk.CariId ,@MId=tblk.MId, @SId= tblk.SId,@Tutar=tblk.Tutar from (
SELECT tbl.CariId , tbl.MId,tbl.SId,tbl.Tutar from (select cb.MId,SUM(bk.Tutar) as Tutar,bk.SId,cb.Id as CariId FROM [BakimKartiDegisenParcalar] bk
join CariBakiye cb on cb.SId=bk.SId
where bk.SId =cb.SId group by bk.SId,cb.MId,cb.Id ) as tbl
) as tblk where SId = @SId
set @Bakiye = @s-@Tutar
update CariBakiye set Borc=@Tutar,Bakiye=@Bakiye where Id=@CariId
print @Id
-- Insert statements for trigger here
END