在记录中加一个“更新时间”字段,每次update的时候更新这个字段为当前时间
set 更新时间=getdate()

解决方案 »

  1.   

    http://expert.csdn.net/Expert/topic/1555/1555076.xml?temp=.9798548
    里说到触发器的编写
    不过,如果数据量大,不提倡用触发器,还不如直接用语句赋值
      

  2.   

    这些方法都太麻烦,能不能在定义的时候实现:
    create table tb_xxx
    (
      id ...
      LastUpdated datetime default(GetDate())
    )这样有没有问题?
      

  3.   

    用触发器是很好的方法:
    USE pubs
    IF EXISTS (SELECT name FROM sysobjects
          WHERE name = 'employee_insupd' AND type = 'TR')
       DROP TRIGGER employee_insupd
    GO
    CREATE TRIGGER employee_insupd
    ON employee
    FOR INSERT, UPDATE
    AS
    /* Get the range of level for this job type from the jobs table. */
    DECLARE @min_lvl tinyint,
       @max_lvl tinyint,
       @emp_lvl tinyint,
       @job_id smallint
    SELECT @min_lvl = min_lvl, 
       @max_lvl = max_lvl, 
       @emp_lvl = i.job_lvl,
       @job_id = i.job_id
    FROM employee e INNER JOIN inserted i ON e.emp_id = i.emp_id 
       JOIN jobs j ON j.job_id = i.job_id
    IF (@job_id = 1) and (@emp_lvl <> 10) 
    BEGIN
       RAISERROR ('Job id 1 expects the default level of 10.', 16, 1)
       ROLLBACK TRANSACTION
    END
    ELSE
    IF NOT (@emp_lvl BETWEEN @min_lvl AND @max_lvl)
    BEGIN
       RAISERROR ('The level for job_id:%d should be between %d and %d.',
          16, 1, @job_id, @min_lvl, @max_lvl)
       ROLLBACK TRANSACTION
    END
      

  4.   

    可以呀!但你:declare @ table(a datetime default(getdate()),b int)
    insert @ values(getdate(),3)
    select * from @
    select * from @
    select * from @
    update @ set b=7,a=default
    select * from @
      

  5.   

    触发器是一个彻底的方案,但过于复杂,大力的方法还比较简单些,不过我希望还能更透明地处理。
    我试验了SQL里的timestamp类型,解决了自动更新标记问题,但它保存的不是时间,是一个整数,能不能规定timestamp的格式?
      

  6.   

    timestamp 类型的数据就是那样的,好象不能改格式吧?
    希望高手给个答案。