我有一个字段,想在在用户添加一个记录的时候,该字段所对应的分量是数据库会自动添加即时时间,且不被更改,连管理员也不行。该怎么做。举个例子,超市收银员刷一下条形码,数据库自动添加该商品被卖出的时间。
平台:sql server 2005补充疑问:
那么这时候,时间是不是只有当系统时间发生改变的时候才会更改,而且是全部更改。

解决方案 »

  1.   

    建个触发器不让它更新:--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
    */
      

  2.   

    上面当更新的时候会产生下面错误:服务器: 消息 50000,级别 16,状态 1,过程 tri_ta,行 9不能修改日期.(所影响的行数为 1 行)数据不会跟着系统的时间改变而改变,如果要更新,那么就要删除触发器
      

  3.   

    1、加入当前时间,不是用now()函数么……(MSSQL是不是now自己查一下吧)2、“不可更改”这个问题……你程序部分,没有修改时间的功能不就好了么……