我有一个字段,想在在用户添加一个记录的时候,该字段所对应的分量是数据库会自动添加即时时间,且不被更改,连管理员也不行。该怎么做。举个例子,超市收银员刷一下条形码,数据库自动添加该商品被卖出的时间。 平台:sql server 2005补充疑问: 那么这时候,时间是不是只有当系统时间发生改变的时候才会更改,而且是全部更改。
建个触发器不让它更新:--1. 建表 create table ta(id int,dt datetime) insert into ta(id,dt) select 1,'2013-08-03 21:55:00' union all select 2,'2013-08-02 16:55:00' union all select 3,'2013-08-01 20:55:00'--2. 建触发器 create trigger tri_ta on ta instead of update as begin if update(dt) begin RAISERROR ('不能修改日期.', 16, 1) end end--3. 更新 update ta set dt=getdate() where id=1select * from ta drop table ta--4. 再查询(结果没有更新) /* 1 2013-08-03 21:55:00.000 2 2013-08-02 16:55:00.000 3 2013-08-01 20:55:00.000 */
create table ta(id int,dt datetime)
insert into ta(id,dt)
select 1,'2013-08-03 21:55:00'
union all select 2,'2013-08-02 16:55:00'
union all select 3,'2013-08-01 20:55:00'--2. 建触发器
create trigger tri_ta
on ta
instead of update
as
begin
if update(dt)
begin
RAISERROR ('不能修改日期.', 16, 1)
end
end--3. 更新
update ta set dt=getdate()
where id=1select * from ta
drop table ta--4. 再查询(结果没有更新)
/*
1 2013-08-03 21:55:00.000
2 2013-08-02 16:55:00.000
3 2013-08-01 20:55:00.000
*/