declare @str varchar(8000)
set @str=''
select top 5 identity(int,1,1) id into #t1 from sysobjects
select * into #t2 from #t1 order by newid()
select @str=@str+'-'+cast(id as varchar) from #t2
set @str=right(@str,len(@str)-1)
drop table #t1
drop table #t2
print @str

解决方案 »

  1.   

    下面这个改一改就可以了declare @i int,@v varchar(50)
    set @i=0
    set @v=''
    while @i<6 
     begin
       set @v=@v+ left(cast(rand()*6 as varchar),1) +'-'
       set @i=@i+1
     end
    print @v
      

  2.   

    declare @Str char(5)
    declare @chr char
    declare @outStr nvarchar(50)
    SET @str='12345'
    declare @i int,@j intSET @outStr=''
    set @i=0
    while @i<5
    BEGIN
    SET @j=cast((rand()*1000) as int)%(5-@i)+1
    SET @chr=RIGHT(LEFT(@Str,@j),1)
    SET @str=replace(@Str,@chr,'')
    if @i>0 and @i<5
    BEGIN
    SET @outStr=@outstr+'-'+@chr
    END
    ELSE
    BEGIN
    SET @outStr=@outStr+@chr
    END
    SET @i=@i+1
    ENDprint @outstr