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
 Check Constraint

Author  Topic 

JR83
Starting Member

31 Posts

Posted - 2014-08-07 : 08:34:25
I want to incorporate a Check constraint within a trigger, based on this but im struggling with the coding.

Assuming that is an Alphanumeric field you should be checking what value is in the alphanumeric column from inserted, comparing it with whatever is in the AMGR_User_Fields_Tbl to see if there’s a duplicate and then raising the error

This is my Trigger:

ALTER TRIGGER [dbo].[DUPLICATES]
ON [dbo].[AMGR_User_Fields_Tbl]

FOR INSERT, UPDATE
AS
DECLARE @Alphanumericcol VARCHAR (750)

-- This trigger has been created to check that duplicate rows are not inserted into table.
-- Check if row exists
SELECT @Alphanumericcol
FROM Inserted i, AMGR_User_Fields_Tbl t
WHERE t.AlphaNumericCol = i.AlphaNumericCol
AND t.Client_Id = i.Client_Id
-- (@Alphanumericcol = 1)
-- Display Error and then Rollback transaction
BEGIN
RAISERROR ('This row already exists in the table', 16, 1)
ROLLBACK TRANSACTION
END

bitsmed
Aged Yak Warrior

545 Posts

Posted - 2014-08-07 : 10:01:23
Duplicate of [url]http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=195602[/url]
Go to Top of Page
   

- Advertisement -