I want a SQL trigger to fire when a table is updated. I have a table called SimActivation and a stored procedure called spUpdateMnpDate that takes two parameters, msisdn(varchar) and date(datetime).
When the SimActivation table is updated I want to call the procedure with the new values.
Here is what I got at the moment, after changing it back and forth:
USE StoreSale;
GO
CREATE TRIGGER dbo.tSyncMnpDate
ON [dbo].[SimActivation]
AFTER UPDATE
AS
DECLARE @date datetime
DECLARE @msisdn varchar(10)
SET @date = (select ProcessDate from inserted)
SET @msisdn = (select Msisdn from inserted)
EXEC [spUpdateMnpDate]
@msisdn, @date;
GO
Can anyone point me in the right direction?
Thanks 🙂
The problem you have is that a trigger will fire when one or more rows have been updated. At the moment you are assuming your trigger will fire for each row, which is not the case in SQL Server.
If the stored procedure you are trying to call is fairly simple I’d pull the code out of there and in to the trigger. But remember you are working with sets of changed rows (even if the change is to only one row) so you have to write your SQL accordingly.
EDIT: I assume your procedure is updating a date where the PK is equal to
@msisdn, if so you can do this in your trigger:Joining the tables ensures it will work for one or many updated rows.