建立存储过程,ID为四位数,产生序列号(Serial)为四位随机数+ID.求助各位高手,改怎么写这个存储过程~

解决方案 »

  1.   


    create proc testpro(@id int)
    as
    begin
    select ltrim(cast(RAND()*10000 as int))+ltrim(@id)
    endexec testpro 1111
      

  2.   

    如果是字符的话,也可以用newid()函数产生一个GUID,然后从中截取.
      

  3.   

    SELECT LEFT(ABS(checksum(newID())),4)--4位
      

  4.   

    SELECT ABS(checksum(newID()))%10000--4位
      

  5.   

    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