I am trying to write an sql trigger that will update another table whenever an update is executed. This is actually used to replace replication (replication exists on our production environment but not on dev environment because it causes problems on dev). I've written following trigger to achieve this, but it doesn't seem to work and I have no idea why. (Also I have no idea how to debug triggers)
USE [AuditQuestionnaireManagerDemo]
GO
/****** Object: Trigger [dbo].[CatchUpdateUser] Script Date: 01/04/2012 09:25:40 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER TRIGGER [dbo].[CatchUpdateUser]
ON [dbo].[User]
FOR UPDATE
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
update SharedServicesDemo.dbo.[User]
set SharedServicesDemo.dbo.[User].Username = Username,
SharedServicesDemo.dbo.[User].PasswordHash = PasswordHash,
SharedServicesDemo.dbo.[User].Salt = Salt,
SharedServicesDemo.dbo.[User].PersonGUID = PersonGUID,
SharedServicesDemo.dbo.[User].OrganizationGUID = OrganizationGUID,
SharedServicesDemo.dbo.[User].IsDeleted = IsDeleted,
SharedServicesDemo.dbo.[User].IsActive = IsActive
where SharedServicesDemo.dbo.[User].GUID = GUID
SELECT GUID, ID, Username, PasswordHash, Salt, PersonGuid, OrganizationGUID, IsDeleted, IsActive From Inserted
END
Would be great if anyone could spot the problem here!
This insert trigger did work with the insert/select combo:
SET IDENTITY_INSERT SharedServicesDemo.dbo.[User] ON
INSERT INTO SharedServicesDemo.dbo.[User] (GUID, ID, Username, PasswordHash, Salt, PersonGUID, OrganizationGUID, IsDeleted, IsActive)
SELECT GUID, ID, Username, PasswordHash, Salt, PersonGuid, OrganizationGUID, IsDeleted, IsActive From Inserted
SET IDENTITY_INSERT SharedServicesDemo.dbo.[User] OFF