T-SQL相当于= rand()

前端之家收集整理的这篇文章主要介绍了T-SQL相当于= rand()前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有几个内容表,我想填写随机的文本段落.在MS Word中,我可以简单地把= rand()和presto!我得到了三段新鲜的新闻文本.

是否有一个sql脚本/命令,我可以使用t-sql生成随机字典单词?

解决方法

;
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,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)
原文链接:https://www.f2er.com/mssql/77268.html

猜你在找的MsSQL相关文章