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 2005 Forums
 Transact-SQL (2005)
 Question setting CONSTRAINTS

Author  Topic 

drpcken
Starting Member

20 Posts

Posted - 2009-03-08 : 13:25:25
I have two fields in one of my tables, a bit called AllowClientAccess that defaults to 0 and a nvarchar called ClientPassword that can be null.

I only want to be able to update ClientPassword if AllowClientAccess = 1 (or > 0 I guess). Is this possible?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-08 : 13:34:18
yup.you need to create a INSTEAD OF UPDATE trigger on table to check this before updation and raise an error if AllowClientAccess =0. something like



CREATE TRIGGER CheckUpdates
ON yourtable
INSTEAD OF UPDATE
AS
BEGIN
UPDATE t
SET t.ClientPassword =i.ClientPassword
FROM YourTable t
JOIN INSERTED i
ON i.PK=t.PK
WHERE t.AllowClientAccess >0

IF EXISTS (SELECT 1 FROM YourTable t
JOIN INSERTED i
ON i.PK=t.PK
WHERE t.AllowClientAccess =0)
RAISERROR ('Cant update client password for records with alloclientaccess set to 0',
10, 1)
END

PK is primary key of your table

Also i think it would be better to enforce this rule in your front end application rather than by trigger as performance might impact for bulk updates
Go to Top of Page
   

- Advertisement -