create table test(tt varchar(5))insert into test 
select '08:00'
union all
select '08:00'
union all
select '08:00'
union all
select '08:00'
现在要实现的是
tt      aa (效果)
08:00   08:01
08:00   08:02
08:00   08:10
08:00   08:22要求就是后列要随机生成的。以前我是用游标,但速度不够快。

解决方案 »

  1.   

    --如果固定是08開頭create table test(tt varchar(5))insert into test 
    select '08:00'
    union all
    select '08:00'
    union all
    select '08:00'
    union all
    select '08:00'
    GO
    Select
    tt,
    '08:' + Right(100 + Cast(Rand(CheckSUM(NewID())) * 59 As Int), 2) As aa
    From
    test
    GO
    Drop Table test
    --Result
    /*
    tt aa
    08:00 08:21
    08:00 08:36
    08:00 08:38
    08:00 08:20
    */
      

  2.   

    --如果tt的內容不是固定的,以tt列做計算的話create table test(tt varchar(5))insert into test 
    select '08:00'
    union all
    select '08:00'
    union all
    select '08:00'
    union all
    select '08:00'
    GOSelect
    tt,
    Convert(Varchar(5), DateAdd(mi, Cast(Rand(CheckSUM(NewID())) * 59 As Int), '1900-01-01 ' + tt), 108) As aa
    From
    test
    GO
    Drop Table test
    --Result
    /*
    tt aa
    08:00 08:21
    08:00 08:38
    08:00 08:27
    08:00 08:30*/
      

  3.   

    xikboy(狼面书生) ( ) 信誉:100    Blog   加为好友  2007-04-18 14:11:24  得分: 0  
     
     
       可以重复,但要随机的。
      
     
    ------
    那就OK。看你的實際情況,取我寫的兩種情況中的一種吧。
      

  4.   

    --或者這樣也可create table test(tt varchar(5))insert into test 
    select '08:00'
    union all
    select '08:00'
    union all
    select '08:00'
    union all
    select '08:00'
    GO
    Select
    tt,
    Left(tt, 3) + Right(100 + Cast(Rand(CheckSUM(NewID())) * 59 As Int), 2) As aa
    From
    test
    GO
    Drop Table test
    --Result
    /*
    tt aa
    08:00 08:32
    08:00 08:05
    08:00 08:21
    08:00 08:26
    */
      

  5.   

    declare @t table(t varchar(5))
    insert into @t 
    select '08:00'
    union all
    select '08:00'
    union all
    select '08:00'
    union all
    select '08:00'
    select t,left(t,3)+replace(str(right(checksum(newid()),2)%60,2),' ','0') aa from @t
      

  6.   

    --可以改寫為函數,但是由於限制,只能這麼調用。create table test(tt varchar(5))insert into test 
    select '08:00'
    union all
    select '08:00'
    union all
    select '08:00'
    union all
    select '08:00'
    GO
    --建立函數
    Create Function F_TEST(@tt varchar(5), @NewID Float)
    Returns Varchar(5)
    As
    Begin
    Return Left(@tt, 3) + Right(100 + Cast(@NewID * 59 As Int), 2)
    End
    GO
    --調用
    Select
    tt,
    dbo.F_TEST(tt, Rand(CheckSUM(NewID()))) As aa
    From
    test
    GO
    Drop Table test
    Drop Function F_TEST
    --Result
    /*
    tt aa
    08:00 08:55
    08:00 08:25
    08:00 08:20
    08:00 08:05
    */