- SQL code
CREATE TRIGGER TR_INSERTMPURTD05 ON [dbo].[PURTD] FOR INSERTASDECLARE @TD001 CHAR(4), @TD002 CHAR(11), @TD003 CHAR(4), @TD004 CHAR(20), @TC004 CHAR(10), @SL NUMERIC(16,6) BEGIN TRANSACTIONSET NOCOUNT ONSELECT @TD001=TD001,@TD002=TD002,@TD004=TD004,@TD003=TD003 FROM INSERTEDSELECT @SL=SUM(TD008)-TD015 FROM PURTD WHERE [email protected] AND TD016='N' UPDATE PURTD SET [email protected] WHERE [email protected] AND [email protected] AND [email protected]SET NOCOUNT OFFCOMMIT TRANSACTION
- SQL code
消息 8120,级别 16,状态 1,过程 TR_INSERTMPURTD05,第 16 行选择列表中的列 'PURTD.TD015' 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中。
请问各位大大这应该怎么改
------解决方案--------------------
SELECT @SL=SUM(TD008)-sum(TD015) FROM PURTD WHERE [email protected] AND TD016='N'
------解决方案--------------------
改用存储过程吧!触发器后期极难维护!
------解决方案--------------------
- SQL code
CREATE TRIGGER TR_INSERTMPURTD05 ON [dbo].[PURTD] FOR INSERTASDECLARE @TD001 CHAR(4), @TD002 CHAR(11), @TD003 CHAR(4), @TD004 CHAR(20), @TC004 CHAR(10), @SL NUMERIC(16,6) BEGIN TRANSACTIONSET NOCOUNT ONSELECT @TD001=TD001,@TD002=TD002,@TD004=TD004,@TD003=TD003 FROM INSERTEDSELECT @SL=SUM(TD008)-TD015 FROM PURTD WHERE [email protected] AND TD016='N' --这句话有错/*如果楼主一定要这么写,那就得在后面出现group by TD015*/UPDATE PURTD SET [email protected] WHERE [email protected] AND [email protected] AND [email protected]SET NOCOUNT OFFCOMMIT TRANSACTION
------解决方案--------------------
用1楼或3楼的方法,程序可以运行,但结果不同,另外,没有考虑到可以一次插入多条记录的情况,inserted表可以有多行的情形.
------解决方案--------------------
sum需要GROUP BY语句