|
-
Aug 8th, 2002, 08:20 AM
#1
Thread Starter
Lively Member
Bit of advice with my Triggers
Hiya
I've created a Trigger on a Customers Table within my SQL Server 2k db that checks for an Entry with the same name. If one si found thenit rolls back the transaction to my app
Alter TRIGGER chk_for_duplicate_cust ON [dbo].[tbl_cust]
FOR INSERT
AS
DECLARE @name varchar(100), @count int
SELECT @name = i.CustomerName
FROM Inserted i
SELECT @count = COUNT(c.[CustomerName])
FROM tbl_cust c
WHERE c.CustomerName = @name
IF (@count > 1)
BEGIN
RAISERROR ('The Customer %s already Exists, 16, 1, @name)
ROLLBACK TRAN
END
This works fine but it is pretty hacked together (i.,e. the count has to be >1)
Is there a better way of writing the Trigger SQL perhaps using the EXISTS statement?
Any ideas would be great
cheers
Tom
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|