求解,sql server如何实现用上一行的余额+借方金额-贷方余额自动填充余额?能否使用触发器实现?上代码
6条回答 默认 最新
- 游一游走一走 2022-10-09 06:48关注
可以的,和正常的写法差不多,只是记得取上一条记录的时候用额外表的一行加个写锁,或者把这个表的第一行加写锁
-- Description: <Description,,> -- ============================================= ALTER TRIGGER [dbo].[yuetb_trigger] ON [dbo].[yuetb] AFTER INSERT AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for trigger here DECLARE TEMP_IGNOR CURSOR FOR select top 1 * from dbo.yuetb order by 序列 for update; declare @序列 int; SELECT @序列=序列 FROM inserted; update dbo.yuetb set 余额=(select top 1 余额 from dbo.yuetb where 序列< @序列 order by 序列 DESC )+isnull(借方,0)-isnull(贷方,0) where 序列=@序列; END DEALLOCATE TEMP_IGNOR;
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用