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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 problems with trigger

Author  Topic 

Pinto
Aged Yak Warrior

590 Posts

Posted - 2008-06-11 : 07:19:57
Here's my trigger. It parses ok but when I execute it I get the error Invalid object name 'spPHCR_AuditTriggerUsers'. It is sql 2005. Thanks for any help.

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER TRIGGER [spPHCR_AuditTriggerUsers]
on [dbo].[tblPHCR_Users]
FOR INSERT, UPDATE, DELETE
-- Add the parameters for the stored procedure here
as

DECLARE @System_User char(30)
SET @System_User=SYSTEM_USER

DECLARE @Action VARCHAR(20)

IF EXISTS (SELECT * FROM inserted)
IF EXISTS (SELECT * FROM deleted)
SET @Action = 'UPDATE'
ELSE
SET @Action = 'INSERT'
ELSE
SET @Action = 'DELETE'

INSERT tblPHCR_Users_Audit
(
US_Id,
US_Surname,
US_Firstname,
us_Paynumber,
US_Level,
US_eMail,
US_Event,
US_ActionedBy,
US_DateActioned,
US_ActionTaken
)
SELECT
x.US_Id,
x.US_Surname,
x.US_Firstname,
x.US_Paynumber,
x.US_Level,
x.US_eMail,
x.US_Event,
@SYSTEM_USER,
Getdate(),
x.Trigger_ActionTaken
FROM (
SELECT CASE
WHEN @Action = 'INSERT' THEN 'INSERT'
ELSE 'UPDATE (new value)'
END AS Trigger_ActionTaken,
i.*
FROM inserted AS i

UNION ALL

SELECT CASE
WHEN @Action = 'UPDATE' THEN 'UPDATE (old value)'
ELSE 'DELETE'
END,
d.*
FROM deleted AS d
) AS x

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-06-11 : 07:22:51
Please use the [ code ] and [ /code ] tags when posting code.


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-06-11 : 07:26:48
The error means that you have to CREATE a trigger with same name first, before you can ALTER the trigger.

Change ALTER TRIGGER to CREATE TRIGGER.
CREATE TRIGGER	[spPHCR_AuditTriggerUsers]
ON [dbo].[tblPHCR_Users]
FOR INSERT,
UPDATE,
DELETE
AS

SET NOCOUNT ON

DECLARE @System_User VARCHAR(30),
@Action VARCHAR(20)

SET @System_User = SYSTEM_USER

IF EXISTS (SELECT * FROM inserted)
IF EXISTS (SELECT * FROM deleted)
SET @Action = 'UPDATE'
ELSE
SET @Action = 'INSERT'
ELSE
SET @Action = 'DELETE'

INSERT tblPHCR_Users_Audit
(
US_Id,
US_Surname,
US_Firstname,
us_Paynumber,
US_Level,
US_eMail,
US_Event,
US_ActionedBy,
US_DateActioned,
US_ActionTaken
)
SELECT x.US_Id,
x.US_Surname,
x.US_Firstname,
x.US_Paynumber,
x.US_Level,
x.US_eMail,
x.US_Event,
@SYSTEM_USER,
GETDATE(),
x.Trigger_ActionTaken
FROM (
SELECT CASE
WHEN @Action = 'INSERT' THEN 'INSERT'
ELSE 'UPDATE (new value)'
END AS Trigger_ActionTaken,
i.US_Id,
i.US_Surname,
i.US_Firstname,
i.US_Paynumber,
i.US_Level,
i.US_eMail,
i.US_Event
FROM inserted AS i

UNION ALL

SELECT CASE
WHEN @Action = 'UPDATE' THEN 'UPDATE (old value)'
ELSE 'DELETE'
END,
d.US_Id,
d.US_Surname,
d.US_Firstname,
d.US_Paynumber,
d.US_Level,
d.US_eMail,
d.US_Event
FROM deleted AS d
) AS x




E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

Pinto
Aged Yak Warrior

590 Posts

Posted - 2008-06-11 : 08:22:10
Thanks for the solution.

How do I use code tags ?
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-06-11 : 08:25:43
[ code ] without spaces starts CODE section. End the section with [ /code ] without spaces.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

Pinto
Aged Yak Warrior

590 Posts

Posted - 2008-06-11 : 08:28:20
Thanks. I'll do that in future.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-11 : 08:29:48
or select the code and click # button on top
Go to Top of Page

Pinto
Aged Yak Warrior

590 Posts

Posted - 2008-06-11 : 08:41:19
I am now getting this error.

Additional information: Cannot insert explicit value for identity column in table 'tblPHCR_Users_Audit' when IDENTITY_INSERT is set to OFF.
The statement has been terminated.

Thanks for the info visakh16

Sorted this now - thanks

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-11 : 09:57:03
quote:
Originally posted by Pinto

I am now getting this error.

Additional information: Cannot insert explicit value for identity column in table 'tblPHCR_Users_Audit' when IDENTITY_INSERT is set to OFF.
The statement has been terminated.

Thanks for the info visakh16

Sorted this now - thanks




It seems like you are trying to pass explicit value for insertion to identity field. If you really want to do this you need to set IDENTITY INSERT to on
SET IDENTITY_INSERT <yourtable> ON
your insert query

otherwise dont pass any value for id column

INSERT INTO Table
(
All columns except identity column
)
VALUES

(
....
)
Go to Top of Page
   

- Advertisement -