update table,inserted set fld1=getDate() where table.id=inserted.id
你试一试。

解决方案 »

  1.   

    IF EXISTS (SELECT 1 FROM sysobjects
          WHERE name = 'tr_table1_insert' AND type = 'TR')
       DROP TRIGGER tr_table1_insert
    GO
    CREATE TRIGGER tr_table1_insert 
    ON dbo.table1
    FOR INSERT
    AS
    UPDATE table1 SET fld1 = getdate()
    GO
      

  2.   

    如果只是insert一条数据用缺省就可以
    吧fld1字段缺省设为getdate()
      

  3.   

    方法1.直接在字段的默认值中写上:getdate()方法2.
    用触发器:
    create trigger t_insert on table1
    for insert
    update table1 set fld1=getdate() where fld1 is null
    go注意插入数据的时候,fd1不要赋值
      

  4.   

    to: Rotaxe(程序员)
    =========
    我不能修改表的结构
      

  5.   

    -- 刚才写错了:
    IF EXISTS (SELECT 1 FROM sysobjects
          WHERE name = 'tr_table1_insert' AND type = 'TR')
       DROP TRIGGER tr_table1_insert
    GO
    CREATE TRIGGER tr_table1_insert 
    ON dbo.table1
    FOR INSERT
    AS
    UPDATE a SET fld1 = getdate() FROM table1 a,INSERTED b WHERE a.id = b.id
    GO--你不需要用出发器就可以实现:
    INSERT table1(col1,col2,fld1,..) SELECT 1,2,getdate(),..
      

  6.   

    to: zjcxc(邹建) 
    =====================用 Inserted 来处理是不是更加安全呢
      

  7.   

    INSERT table1(col1,col2,fld1,..) SELECT 1,2,getdate(),..
    好。
      

  8.   

    CREATE TRIGGER tr_table1_insert 
    ON dbo.test
    FOR INSERT
    AS
    update test set col2=getdate() where col1 = (select col1 from inserted)
    ====这个是我参考了大家的意见然后用inserted做的
    这里请问下:inserted表中可能同时存在2条记录吗?
      

  9.   

    可能存在2条记录,如果用Insert into test select ...的话
      

  10.   

    用in就可以
    CREATE TRIGGER tr_table1_insert 
    ON dbo.test
    FOR INSERT
    AS
    update test set col2=getdate() where col1 in (select col1 from inserted)
      

  11.   

    不用触发器不是更好吗?
    insert table1 (fld1) values(getdate())
      

  12.   

    你的表中有主键吗?如果有主键,用inserted表处理更安全.如果没有主键.就只好用我上面的办法啦.
      

  13.   

    --表中有主键时的触发器写法:create trigger t_insert on table1
    for insert
    as
    update table1 set fld1=getdate()
    from table1 a join inserted b on a.主键=b.主键
    go