create proc testpro(@id int) as begin select ltrim(cast(RAND()*10000 as int))+ltrim(@id) endexec testpro 1111
如果是字符的话,也可以用newid()函数产生一个GUID,然后从中截取.
SELECT LEFT(ABS(checksum(newID())),4)--4位
SELECT ABS(checksum(newID()))%10000--4位
create table tb(id int,flg int,sno as right('000'+ltrim(id),4)+right('000'+ltrim(flg),4)) go insert into tb(id,flg) select 1,floor(rand()*10000+1) insert into tb(id,flg) select 230,floor(rand()*10000+1) select * from tb /* id flg sno ----------- ----------- ---------------- 1 3384 00013384 230 4398 02304398(2 行受影响)*/ go drop table tb
create proc testpro(@id int)
as
begin
select ltrim(cast(RAND()*10000 as int))+ltrim(@id)
endexec testpro 1111
go
insert into tb(id,flg) select 1,floor(rand()*10000+1)
insert into tb(id,flg) select 230,floor(rand()*10000+1)
select * from tb
/*
id flg sno
----------- ----------- ----------------
1 3384 00013384
230 4398 02304398(2 行受影响)*/
go
drop table tb