=rand() 的 T-SQL 等价物
Posted
技术标签:
【中文标题】=rand() 的 T-SQL 等价物【英文标题】:T-SQL equivalent of =rand() 【发布时间】:2010-11-10 17:38:25 【问题描述】:我有几个内容表,我想用随机的文本段落填充它们。在 MS Word 中,我可以简单地输入 =rand() 和 presto!我得到了三段新鲜的文本。
是否有可以使用 t-sql 生成随机字典单词的 SQL 脚本/命令?
【问题讨论】:
这是一个巧妙的技巧。 word 中是否有其他可用的 =func() 列表? 这里有更多关于 rand()/lorem() 的信息,供好奇的人(就像我一样)support.microsoft.com/kb/212251 【参考方案1】:
;
declare
@Lorem nvarchar(max),
@RowsToGen int,
@Factor int
select
@Lorem = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
@RowsToGen = 200
-- strip punctuations
set @Lorem = replace(@Lorem, ',', '')
set @Lorem = replace(@Lorem, '.', '')
;
with
Num1(Pos) as
(
select cast(1 as int)
union all
select cast(Pos + 1 as int) from Num1 where Pos < len(@Lorem)
),
Words as
(
select substring(@Lorem, Pos, charindex(' ', @Lorem + ' ', Pos) - Pos) as Word
from Num1 where Pos <= len(@Lorem) and substring(',' + @Lorem, Pos, 1) = ' '
),
WordsCnt(Factor) as
(
select @RowsToGen / count(*) + 1 from Words
),
Num2(Pos) as
(
select cast(1 as int)
union all
select cast(Pos + 1 as int) from Num2 cross join WordsCnt where Pos < WordsCnt. Factor
)
select top (@RowsToGen)
Word
from
Num2
cross join
Words
order by newid()
option (maxrecursion 0)
【讨论】:
我喜欢这个。我如何将其旋转为每行 20 个单词而不是每行一个单词? @Raj More,抱歉不在了,请在底部找到“句子”版本。【参考方案2】:根据 Raj More 的要求,这是另一个版本,它给出一个句子而不是每行一个单词。
;
declare
@Lorem nvarchar(max),
@SentenceToGen int,
@WordsPerSentence int,
@Factor int
select
@Lorem = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
@SentenceToGen = 200,
@WordsPerSentence = 10
-- strip punctuations
set @Lorem = replace(@Lorem, ',', '')
set @Lorem = replace(@Lorem, '.', '')
;
with
Num1(Pos) as -- number of chars in @Lorem
(
select cast(1 as int)
union all
select cast(Pos + 1 as int) from Num1 where Pos < len(@Lorem)
),
Words as
(
select lower(substring(@Lorem, Pos, charindex(' ', @Lorem + ' ', Pos) - Pos)) as Word
from Num1 where Pos <= len(@Lorem) and substring(',' + @Lorem, Pos, 1) = ' '
),
WordsCnt(Factor) as
(
select ceiling(cast(@SentenceToGen * @WordsPerSentence as float) / count(*)) from Words
),
Num2(Pos) as -- product of words required, to be divided by number of words found in @Lorem
(
select cast(1 as int)
union all
select cast(Pos + 1 as int) from Num2 cross join WordsCnt where Pos < WordsCnt.Factor
),
Sentences as
(
select
ntile(@SentenceToGen) over (order by newid()) as SentenceId,
Word
from
Num2
cross join
Words
),
Num3(Pos) as -- list of SentenceId
(
select distinct SentenceId from Sentences
)
select
(
select top (@WordsPerSentence)
Word + ' '
from
Sentences
where
Sentences.SentenceId = Num3.Pos
for xml path('')
)
as Sentence
from
Num3
option (maxrecursion 0)
【讨论】:
【参考方案3】:没有内置任何东西,但很容易做到:
create table DictionaryWords
(
Id int primary key identity (1,1),
Word nvarchar(100) not null
)
go
insert DictionaryWords values ('the')
insert DictionaryWords values ('quick')
insert DictionaryWords values ('brown')
insert DictionaryWords values ('fox')
insert DictionaryWords values ('jumped')
insert DictionaryWords values ('over')
insert DictionaryWords values ('the')
insert DictionaryWords values ('lazy')
insert DictionaryWords values ('dog')
go
create procedure dbo.CreateRandomText(@numWords int, @text nvarchar(max) out)
as
begin
declare @rowcount int
select @rowcount = count(*) from DictionaryWords
select @text = ''
while @numWords <> 0
select @text = @text + ' ' + Word, @numWords = @numWords - 1
from DictionaryWords
where Id = cast(@rowcount * rand() as integer)
end
go
declare @text nvarchar(max)
exec CreateRandomText 10, @text out
select @text
【讨论】:
以上是关于=rand() 的 T-SQL 等价物的主要内容,如果未能解决你的问题,请参考以下文章