日期:2014-05-18 浏览次数:20827 次
CREATE TRIGGER TR_INSERTMPURTD05 ON [dbo].[PURTD] FOR INSERT AS DECLARE @TD001 CHAR(4), @TD002 CHAR(11), @TD003 CHAR(4), @TD004 CHAR(20), @TC004 CHAR(10), @SL NUMERIC(16,6) BEGIN TRANSACTION SET NOCOUNT ON SELECT @TD001=TD001,@TD002=TD002,@TD004=TD004,@TD003=TD003 FROM INSERTED SELECT @SL=SUM(TD008)-TD015 FROM PURTD WHERE TD004=@TD004 AND TD016='N' UPDATE PURTD SET UDF56=@SL WHERE TD001=@TD001 AND TD002=@TD002 AND TD003=@TD003 SET NOCOUNT OFF COMMIT TRANSACTION
消息 8120,级别 16,状态 1,过程 TR_INSERTMPURTD05,第 16 行 选择列表中的列 'PURTD.TD015' 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中。
CREATE TRIGGER TR_INSERTMPURTD05 ON [dbo].[PURTD] FOR INSERT AS DECLARE @TD001 CHAR(4), @TD002 CHAR(11), @TD003 CHAR(4), @TD004 CHAR(20), @TC004 CHAR(10), @SL NUMERIC(16,6) BEGIN TRANSACTION SET NOCOUNT ON SELECT @TD001=TD001,@TD002=TD002,@TD004=TD004,@TD003=TD003 FROM INSERTED SELECT @SL=SUM(TD008)-TD015 FROM PURTD WHERE TD004=@TD004 AND TD016='N' --这句话有错 /* 如果楼主一定要这么写,那就得在后面出现group by TD015 */ UPDATE PURTD SET UDF56=@SL WHERE TD001=@TD001 AND TD002=@TD002 AND TD003=@TD003 SET NOCOUNT OFF COMMIT TRANSACTION
------解决方案--------------------
用1楼或3楼的方法,程序可以运行,但结果不同,另外,没有考虑到可以一次插入多条记录的情况,inserted表可以有多行的情形.
------解决方案--------------------
sum需要GROUP BY语句