id(人员编号) 入职日期(date)
07010101 20070101
07010102 20070101
08121201 20081212
如上 人员编号为入职日期后六位加两位随机数 ,同一天入职的编号要加一(保证人员编号不同)
数据很多的 ,如何update啊update table set id=。不会
07010101 20070101
07010102 20070101
08121201 20081212
如上 人员编号为入职日期后六位加两位随机数 ,同一天入职的编号要加一(保证人员编号不同)
数据很多的 ,如何update啊update table set id=。不会
--六位年份+两位随机数呢,可以这样写,其他你看着办就好了,不清楚你要做什么
select right(convert(varchar(8),getdate(),112),6)+right(convert(varchar(20),rand()),2);
set id= right('00000000'+ltrim(abs(CHECKSUM(NEWID()))),8)
select right(convert(varchar(8),getdate(),112),6)+right(convert(varchar(20),rand()),2);最后2位从01开始的 有相同的话变成02。03。。04。。09。10。
别rand了
upupupupup
DECLARE @i VARCHAR(10)select @i=MAX(人员编号)FROM TB WHERE 入职日期='20081212'
IF @i IS NOT NULL
INSERT dbo.tb ( 人员编号, 入职日期 )
VALUES ( suijishu,20081212)
ELSE
INSERT dbo.tb ( 人员编号, 入职日期 )
VALUES (LEFT(@i,6)+RIGHT('000'+RIGHT(@i,2)+1,2),20081212)
id(人员编号) 入职日期(date)
07010101 20070101
07010102 20070101
07010103 20070101
08121201 20081212如何更新id 得到上面的id值
goif object_id('T') is not null
drop table T
go
create table T(Code varchar(8))
go
create trigger tr_T_insert on T
instead of insert
as
set nocount on;
begin
declare @MinCode varchar(6),@MaxNr tinyint
set @MinCode=CONVERT(varchar(6),GETDATE(),112)
set @MaxNr=isnull((select 100+max(right(Code,2)) from T where Code like @MinCode+'__' ),100)
insert into T(Code)select Code=@MinCode+right(@MaxNr+ROW_NUMBER()over(order by @MinCode),2) from inserted end
gogo
--测试
insert T
select 1 union all--这里1为任何值都不影响id插入的值
select 1
go
insert T
select 1
goselect * from T
/*
Code
20120801
20120802
20120803
*/