Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
taniarto
Starting Member
27 Posts |
Posted - 2013-09-27 : 21:58:22
|
Dear All,I have 2 tables :1. Table sales ID item qty001 ICe 20002 Book 10003 Book 202. Table StockID qty001 20002 30I create a trigger on Sales Table :set ANSI_NULLS ONset QUOTED_IDENTIFIER ONgoALTER TRIGGER [insert_sales] ON [dbo].[sales] for INSERT,update ASdeclare @id_ varchar(20)declare @qty intIF EXISTS(SELECT idFROM sales where id=@id )begin update s set s.qty=i.qty from stock s join (select id,sum(qty) as qty from inserted group by id) i on s.id=i.id endthe trigger is running but the result only show the latest record not all of the sum calculation.The result show :ID Qty002 20not 30 ( as all record result)Please help..thanks |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-09-28 : 05:53:12
|
why do need IF EXISTS? isnt below enough?ALTER TRIGGER [insert_sales] ON [dbo].[sales] for INSERT,update ASupdate s set s.qty=i.qty from stock sjoin (select id,sum(qty) as qty from inserted group by id) ion s.id=i.id ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
taniarto
Starting Member
27 Posts |
Posted - 2013-09-30 : 04:02:05
|
If the record were deleted, is it calculated into the new calculation?or we must make a delete trigger too?thanksquote: Originally posted by visakh16 why do need IF EXISTS? isnt below enough?ALTER TRIGGER [insert_sales] ON [dbo].[sales] for INSERT,update ASupdate s set s.qty=i.qty from stock sjoin (select id,sum(qty) as qty from inserted group by id) ion s.id=i.id ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
|
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-09-30 : 06:20:22
|
quote: Originally posted by taniarto If the record were deleted, is it calculated into the new calculation?or we must make a delete trigger too?thanksquote: Originally posted by visakh16 why do need IF EXISTS? isnt below enough?ALTER TRIGGER [insert_sales] ON [dbo].[sales] for INSERT,update ASupdate s set s.qty=i.qty from stock sjoin (select id,sum(qty) as qty from inserted group by id) ion s.id=i.id ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
current logic doesnt account deletesif you want to include it too make trigger likeALTER TRIGGER [insert_sales] ON [dbo].[sales] for INSERT,update ,DeleteASupdate s set s.qty=s.qty + i.qty - d.qty from stock sleft join (select id,sum(qty) as qty from inserted group by id) ion s.id=i.id left join (select id,sum(qty) as qty from deleted group by id) don s.id=d.id ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-09-30 : 08:36:12
|
You need a MERGE, because what happens when you delete the last row from Sales?ALTER TRIGGER dbo.trgSalesON dbo.Sales AFTER INSERT, UPDATE, DELETEASSET NOCOUNT ON;MERGE dbo.Stock AS tgtUSING ( SELECT ID, SUM(Qty) AS Qty FROM dbo.Sales GROUP BY ID ) AS src ON src.ID = tgt.IDWHEN MATCHED AND tgt.Qty <> src.Qty THEN UPDATE SET tgt.Qty = src.QtyWHEN NOT MATCHED BY TARGET THEN INSERT ( ID, Qty ) VALUES ( src.ID, src.Qty )WHEN NOT MATCHED BY SOURCE THEN DELETE; Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
|
|
|
|
|