SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Triggers


Triggers

Author
Message
MSSQL_NOOB
MSSQL_NOOB
SSC Veteran
SSC Veteran (265 reputation)SSC Veteran (265 reputation)SSC Veteran (265 reputation)SSC Veteran (265 reputation)SSC Veteran (265 reputation)SSC Veteran (265 reputation)SSC Veteran (265 reputation)SSC Veteran (265 reputation)

Group: General Forum Members
Points: 265 Visits: 173
SQL2000

Need help with triggers. I'm trying to create a trigger that checks the table; if the record exists based on a few criterias, then it won't insert, otherwise insert it.

This is my attempt following http://www.sqlservercentral.com/articles/Triggers/64214


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'BI_XYZ_TRI' AND type = 'TR')
DROP TRIGGER BI_XYZ_TRI
GO

CREATE TRIGGER BI_XYZ_TRI
ON XYZ
INSTEAD OF INSERT
AS

BEGIN
SET NOCOUNT ON;
IF NOT EXISTS (SELECT * FROM inserted a JOIN xyz b
ON a.criteria1 = b.criteria1
AND a.criteria2 = b.criteria2
AND a.criteria3 = b.criteria1
AND a.criteria4 = b.criteria1
AND a.criteria5 = b.criteria5)
BEGIN
INSERT INTO dbo.xyz
( col1,
col2,
col3,
col4,
col5,
...
)
SELECT col1,
col2,
col3,
col4,
col5,
...
FROM inserted
END -- ends insert sttmt
ELSE
BEGIN
RAISERROR ('Duplicate Records')
END -- ends raiserror
END -- ends trigger

GO


I'm getting error
Incorrect syntax near ')'


Can anyone help? Thank you
Lowell
Lowell
SSC Guru
SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)

Group: General Forum Members
Points: 71878 Visits: 40942
pretty sure RAISERROR requires three parameters, and you only passed one.
RAISERROR ('Duplicate Records',16,1)

Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search