首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

如何样用函数或其它办法实现以下功能?(加分帖)

2012-02-15 
怎么样用函数或其它办法实现以下功能?(加分帖)createtabletest(ttvarchar(5))insertintotestselect08:00un

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

热点排行
Bad Request.