TSQL 伪随机文本生成器

问题描述 投票:0回答:5

我正在对 SQL 存储过程进行一些性能测试,只想使用一个快速数据生成器进行测试。

我正在寻找一种简单的方法来生成伪随机(在这种情况下不需要真正的随机)varchar 字段。

到目前为止,我的想法是对可以使用的有效字符进行字符定义,然后根据该定义构建字符串,并使用伪随机长度来定义最大/最小长度的长度变化。

编辑:

我的测试数据生成器:

DECLARE @MyDataTable TABLE
(
  RecID int IDENTITY(1,1) PRIMARY KEY,
  SomeText varchar(255)
)

DECLARE @RecId int, @SomeText varchar(255),
        @maxlength int, @minlength int, 
        @RecordCount int, @Counter int
SET @maxlength = 254
SET @minlength = 50
SET @RecordCount = 500000
SET @Counter = 1

WHILE (@Counter < @RecordCount)
BEGIN
 INSERT INTO @MyDataTable
 (
  SomeText
 )
 SELECT  TOP 1
 ( 
   select top (abs(checksum(newid())) % (@maxlength-@minlength) + @minlength) char(abs(checksum(newid())) % 26 + ascii('A'))  
   from sys.all_objects a1
   where sign(a1.object_id) = sign(t.object_id) /* Meaningless thing to force correlation */
   for xml path('')
 ) as NewRandomString 
 FROM sys.all_objects t;
 SET @Counter = @Counter + 1
END
sql t-sql random
5个回答
11
投票

我最近写了一篇关于此的博客文章。

http://msmvps.com/blogs/robfarley/archive/2009/12/07/randomising-data.aspx

select top (@stringlength) char(abs(checksum(newid())) % 26 + ascii('A')) 
from sys.all_objects 
for xml path('')
;

编辑:抱歉 - 没有包括随机长度的东西......

SELECT 
(
  select top (abs(checksum(newid())) % (@maxlength-@minlength) + @minlength) char(abs(checksum(newid())) % 26 + ascii('A')) 
  from sys.all_objects 
  for xml path('')
) as NewRandomString
FROM yourTable; /* Maybe something like dbo.nums? */

编辑:抱歉 - 需要关联...

SELECT  
( 
  select top (abs(checksum(newid())) % (@maxlength-@minlength) + @minlength) char(abs(checksum(newid())) % 26 + ascii('A'))  
  from sys.all_objects a1
  where sign(a1.object_id) = sign(t.object_id) /* Meaningless thing to force correlation */
  for xml path('')
) as NewRandomString 
,*
FROM sys.all_objects t;

4
投票

对于 SQL Server 2008

SELECT
    --fixed length
    CAST(CRYPT_GEN_RANDOM(50) AS varchar(100)),
    --variable length
    CAST(CRYPT_GEN_RANDOM(ABS(CHECKSUM(NEWID()))%50) AS varchar(100))

样品:

r¡Ñ”ã8Ò¯wß×1W=ýÎÜTÜN:Læ*é=Öô/qAtmտ׌1):¢ìèð’¾N
mÁ­BòºÇòWãmßyWßðÛ2ﬔœ¹t ¦2›ÏÀë?î7Ä›››ºªb

我的邪恶双胞胎想用它作为密码生成器......


2
投票

这将生成一个可变长度的随机字符串。

DECLARE     @text nvarchar(255),
            @length int,
            @i int;
SET @i = 0
SET @text = ''
SET @length = RAND() * 50 + 215
WHILE (@i < @length)
BEGIN
    SET @text = @text + CHAR(RAND() * 26 + 65)
    SET @i = @i + 1
END

1
投票

如果您急需或者不想自己动手, 您还可以使用

中的工具

http://www.generatedata.com/

但是如果您只使用在线演示,则只能生成 100 行。


0
投票

你可以像这样随机化 lorem ipsum 单词,我使用了 64 个单词的字典。


SELECT STRING_AGG(text, ' ')
FROM (
    SELECT top(64) text
    FROM (
        VALUES ('lorem'), ('ipsum'), ('dolor'), ('sit'), ('amet,'), ('consectetur'), ('adipiscing'), ('elit,'), ('sed'), ('do'), ('eiusmod'), ('tempor'), ('incididunt'), ('ut'), ('labore'), ('et'), ('dolore'), ('magna'), ('aliqua.'), ('ut'), ('enim'), ('ad'), ('minim'), ('veniam,'), ('quis'), ('nostrud'), ('exercitation'), ('ullamco'), ('laboris'), ('nisi'), ('aliquip'), ('ex'), ('ea'), ('commodo'), ('consequat.'), ('duis'), ('aute'), ('irure'), ('in'), ('reprehenderit'), ('voluptate'), ('velit'), ('esse'), ('cillum'), ('eu'), ('fugiat'), ('nulla'), ('pariatur.'), ('excepteur'), ('sint'), ('occaecat'), ('cupidatat'), ('non'), ('proident,'), ('sunt'), ('culpa'), ('qui'), ('officia'), ('deserunt'), ('mollit'), ('anim'), ('id'), ('est'), ('laborum')
    ) AS a(text)
    ORDER BY NEWID()
) t

这将在每次运行该查询时返回不同的 lorem ipsum 字符串,稍后,如果要更新多行,则必须创建一个函数

CREATE VIEW getNewID as select newid() as new_id;

CREATE OR ALTER FUNCTION dbo.lorem_ipsum(@length bigint) RETURNS NVARCHAR(max) AS
BEGIN
    DECLARE @return NVARCHAR(MAX)
    SELECT @return = STRING_AGG(text, ' ')
    FROM (
        SELECT top(64) text
        FROM (
            VALUES ('lorem'), ('ipsum'), ('dolor'), ('sit'), ('amet,'), ('consectetur'), ('adipiscing'), ('elit,'), ('sed'), ('do'), ('eiusmod'), ('tempor'), ('incididunt'), ('ut'), ('labore'), ('et'), ('dolore'), ('magna'), ('aliqua.'), ('ut'), ('enim'), ('ad'), ('minim'), ('veniam,'), ('quis'), ('nostrud'), ('exercitation'), ('ullamco'), ('laboris'), ('nisi'), ('aliquip'), ('ex'), ('ea'), ('commodo'), ('consequat.'), ('duis'), ('aute'), ('irure'), ('in'), ('reprehenderit'), ('voluptate'), ('velit'), ('esse'), ('cillum'), ('eu'), ('fugiat'), ('nulla'), ('pariatur.'), ('excepteur'), ('sint'), ('occaecat'), ('cupidatat'), ('non'), ('proident,'), ('sunt'), ('culpa'), ('qui'), ('officia'), ('deserunt'), ('mollit'), ('anim'), ('id'), ('est'), ('laborum')
        ) AS a(text), getNewID
        
        ORDER BY new_id
    ) t
    return upper(substring(@return, 1, 1)) + substring(@return, 2, @length - 1);
END

然后更新您的所有值:

UPDATE my_custom_table
SET my_text_column = dbo.lorem_ipsum(len(my_text_column))
© www.soinside.com 2019 - 2024. All rights reserved.