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
 Locking a column

Author  Topic 

insanepaul
Posting Yak Master

178 Posts

Posted - 2011-11-16 : 07:51:21
I created a table which calculates an hourly target based on the previous hour takings.

The table will always have the same number of rows and each row corresponds to an hour in the day over a period of 4 days.

In the percentage column each row (hour) has a different value which will never change. I was wondering that it might get accidently changed. How would I lock this column to prevent this from happening?

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-11-16 : 08:03:39
Trigger?

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

insanepaul
Posting Yak Master

178 Posts

Posted - 2011-11-16 : 08:16:01
Hi, Not sure what you mean. The previous hour takings is fed into a column which I already use a trigger to calculate the target for the next hour. So the percentage column will not get overwritten but i'm afraid that someone might accidently delete these figures.

quote:
Originally posted by nigelrivett

Trigger?

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.

Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-11-16 : 08:23:42
create trigger PreventChange on tbl for update
as
if exists (select * from inserted i join deleted d on i.pk = d.pk and (i.rate <> d.rate or (d.rate is null and i.rate is not null)))
begin
raiserror ('cannot change rate', 16, -1)
rollback tran
end


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

insanepaul
Posting Yak Master

178 Posts

Posted - 2011-11-16 : 08:32:24
Ah I see now thanks. I would never have thought to use that. I thought it might be a setting on the column. Thanks again.

quote:
Originally posted by nigelrivett

create trigger PreventChange on tbl for update
as
if exists (select * from inserted i join deleted d on i.pk = d.pk and (i.rate <> d.rate or (d.rate is null and i.rate is not null)))
begin
raiserror ('cannot change rate', 16, -1)
rollback tran
end


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.

Go to Top of Page
   

- Advertisement -