update 表
set 编号=right( '0000' + cast(cast((select top 1 编号 from 表 order by 编号 desc) as bigint)+1 as varchar),4)
where 编号 is null or 编号='' --在插入记录时,编号没有值就是null,或者可以插入空字
两种方法:
一.在插入记录前先计算当前表最后一个最大的编号,然后生成一个递增1的编号,然后连同其他字段插入到表中;
二.使用触发器,要求表不能删除记录,如表机构(编号,姓名)
create trigger T on 表
for insert
as
update 表
set 编号=right( '0000' + cast((select count(*) from 表) as varchar),4)
where 编号 is null or 编号='' --在插入记录时,编号没有值就是null,或者可以插入空字符
IDENTITY([, seed increment]) [AS column_name])
--IDENTITY() 函数只在SELECT INTO 语句中使用用于插入一个identity column列到新表中
/*select identity(int, 1, 1) as column_name
into newtable
from oldtable*/