日期:2014-05-19  浏览次数:20545 次

怎么样用函数或其它办法实现以下功能?
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

要求就是后列要随机生成的。

以前我是用游标,但速度不够快。


------解决方案--------------------
alter table test add id int identity(1,1)
go
select tt,left(tt,3)+right( '00 '+rtrim(cast(rand()*(60-id) as int)),2) as aa from test
------解决方案--------------------
alter table test add id int identity(1,1)

SELECT TT,AA=RIGHT(CONVERT(CHAR(16),DATEADD ( minute ,CAST( rand()*60+ID AS INT), TT ),21),5) FROM TEST
------解决方案--------------------
--或者這樣也可

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
*/