读书人

如何样用函数或其它办法实现以下功能?

发布时间: 2012-02-15 12:09:44 作者: rapoo

怎么样用函数或其它办法实现以下功能?(加分帖)
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
/*
ttaa
08:0008:55
08:0008:25
08:0008:20
08:0008:05
*/

读书人网 >SQL Server

热点推荐