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

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

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

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

------解决方案--------------------
要求aa列中不能有重复的吗?
------解决方案--------------------
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
------解决方案--------------------
--可以改寫為函數,但是由於限制,只能這麼調用。


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