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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Trigger syntax

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-10-05 : 09:50:49
Kavita writes "I am pasting below a trigger that I am trying to execute in the query analyzer of sql server 7.0 on windows 2000 and the analyzer returns the following error message, can u please explain me why so that i can correct the syntax in the trigger:

IF EXISTS (SELECT name FROM sysobjects WHERE name = auditTrigger AND type = 'TR')
DROP TRIGGER auditTrigger
GO
CREATE TRIGGER auditTrigger ON dbo.t1 FOR INSERT, UPDATE, DELETE AS SET NOCOUNT ON DECLARE @action CHAR(1) IF NOT EXISTS(SELECT * FROM deleted) SET @action = 'I'ELSE IF EXISTS(SELECT * FROM inserted) SET @action = 'U'ELSE SET @action = 'D'IF @action = 'U' OR @action = 'D' BEGIN INSERT ztable_audit SELECT *, @action, CASE WHEN @action = 'U' THEN 'OLD' ELSE NULL END, CONVERT(char(30),CURRENT_TIMESTAMP), NULL, SUSER_SNAME(), SYSTEM_USER, HOST_NAME(), 'WHERE EMPLOYEE_ID='+CAST(del.EMPLOYEE_ID AS VARCHAR), 't1'FROM deleted del END IF @action = 'U' OR @action = 'I' BEGIN INSERT ztable_audit SELECT *, @action, CASE WHEN @action = 'U' THEN 'NEW' ELSE NULL END,CONVERT(char(30),CURRENT_TIMESTAMP), NULL, SUSER_SNAME(),SYSTEM_USER, HOST_NAME(), 'WHERE EMPLOYEE_ID='+CAST(ins.EMPLOYEE_ID AS VARCHAR), 't1' FROM inserted ins END

The error messages i get are the following:

Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'GO'.
Server: Msg 111, Level 15, State 1, Line 1
'CREATE TRIGGER' must be the first statement in a query batch.

can u please let me know as to what am I doing wrong?

Thanks,

Kavita"
   

- Advertisement -