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 |
|
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 ONset QUOTED_IDENTIFIER ONgo-- =============================================-- 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 hereas 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_ActionTakenFROM ( 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" |
 |
|
|
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, DELETEASSET NOCOUNT ONDECLARE @System_User VARCHAR(30), @Action VARCHAR(20)SET @System_User = SYSTEM_USERIF 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_ActionTakenFROM ( 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" |
 |
|
|
Pinto
Aged Yak Warrior
590 Posts |
Posted - 2008-06-11 : 08:22:10
|
| Thanks for the solution.How do I use code tags ? |
 |
|
|
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" |
 |
|
|
Pinto
Aged Yak Warrior
590 Posts |
Posted - 2008-06-11 : 08:28:20
|
| Thanks. I'll do that in future. |
 |
|
|
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 |
 |
|
|
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 visakh16Sorted this now - thanks |
 |
|
|
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 visakh16Sorted 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 onSET IDENTITY_INSERT <yourtable> ONyour insert queryotherwise dont pass any value for id columnINSERT INTO Table(All columns except identity column)VALUES(....) |
 |
|
|
|
|
|
|
|