---若:select:
select id,right(10000+id,4)as noid from table
----只能在id小于10000的情况,大于10000的情况不知道楼主想如果处理----如是update
update table set noid=right(10000+id,4)
----只能在id小于10000的情况,大于10000的情况不知道楼主想如果处理

解决方案 »

  1.   

    create table tb1
    (
     id int identity(1,1) primary key,
     noid varchar(20))reate trigger autoadd
    on tb1
    for insert
    as
     declare @noid varchar(20),@id int,@strid varchar(10)
     set @id=(select id from inserted)
     set @strid=cast(@id as varchar(10))
     set @noid=substring('0000',1,4-len(@strid)) + @strid
     print @noid
     update tb1 set noid=@noid where id=@id 
      

  2.   

    --用触发器来实现自动更新.--创建测试表
    create table test(id int identity(1,1),noid varchar(4),[name] varchar(100))
    --创建触发器
    create trigger dbo.tri_test
    on test for insert
    as
    update test
    set noid=right(10000+t.id,4)
    from test t join inserted i
    on t.id=i.idgo
    --插入数据
    insert test([name])
    select '张三'
    union all select '李四'
    union all select '王五'
    --结果
    /*
    id          noid name                                                                                                 1           0002 张三
    2           0003 李四
    3           0004 王五(所影响的行数为 3 行)