Click here to Skip to main content
15,915,324 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a table like this
SQL
create table cus(
username varchar(20) primary key,
password varchar(50)
);

My business requirement is such that i should not be able to edit the username field. What i mean is i need to rollback a transaction if there is an update statement.
What i do is i create a trigger something like this
SQL
create trigger cusonupdate on dbo.cus
for update 
as
rollback
print'The suggested Row cannot be updated'

This will do the required thing. But what i need is i must be having a provision to update the password. How should i do it?
Posted

Try this way -

SQL
create trigger cusonupdate on dbo.cus
for update
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    IF UPDATE(username)
        BEGIN
            ROLLBACK
            print'The suggested Row cannot be updated'
        END

END
 
Share this answer
 
Comments
Rakshith Kumar 15-Oct-13 3:29am    
Its working :). Thanks a ton
Hello freind you can use DENY on update with your cus table, something like this...

SQL
DENY UPDATE ON dbo.cus (username ) TO <database username>


with DENY keywork you need to specify sql server user for which you need to DENY update

Referance :
DENY[^]
 
Share this answer
 
v2
Comments
Rakshith Kumar 15-Oct-13 3:32am    
Nice one tejas :).
Tejas Vaishnav 15-Oct-13 4:31am    
Thanks
Madhu Nair 15-Oct-13 4:34am    
Very Good!!!!
Tejas Vaishnav 15-Oct-13 4:38am    
Thanks
Rakshith Kumar 15-Oct-13 5:17am    
My dear i guess its only for the sql server users ? Correct me if iam wrong. The thing is i have a table customer and i need to set the username as un editable. denying the update previlege will mean that you are denying the sql server user the previlege to edit.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900