介绍 (Introduction)

In this article, we will talk about generating random values for testing purposes.

在本文中,我们将讨论为测试目的生成随机值。

I once had a customer with software that worked fine in the demo with 30 rows, but after some months, the software had more than a million rows and it became very slow. The problem was not SQL Server, the problem was the application, which was not designed for tables with millions of rows. The customer sued to the software provider and lawyers were needed to create a resolution. If the provider had tested the software with millions of rows, this problem would have never happened.

我曾经有一个客户,该客户的软件在演示中可以正常运行30行,但几个月后,该软件已超过一百万行,并且变得非常慢。 问题不是SQL Server,问题是应用程序,它不是为具有数百万行的表设计的。 客户起诉了软件提供商,需要律师来制定解决方案。 如果提供者已经对软件进行了数百万行的测试,那么这个问题将永远不会发生。

That is why, it is very important to generate data and test the software with millions of rows. This is not always an easy task. In this article, we will give you some useful T-SQL tips that may help or at least inspire you on this. In general, random data is very useful for testing purposes, to learn about query efficiency, demos and more.

因此,生成数据并测试数百万行的软件非常重要。 这并不总是一件容易的事。 在本文中,我们将为您提供一些有用的T-SQL技巧,这些技巧可能会帮助或至少启发您。 通常,随机数据对于测试目的,了解查询效率,演示等内容非常有用。

In this article, we will teach how to generate up to a million rows of random data in SQL Server including:

在本文中,我们将讲授如何在SQL Server中生成多达一百万行的随机数据,包括:

  1. combinations of user names and last names 用户名和姓氏的组合
  2. integer values 整数值
  3. real numbers with a specific range 特定范围的实数
  4. passwords in SQL Server SQL Server中的密码
  5. emails 电邮
  6. country names 国名

要求 (Requirements)

  1. SQL Server SQL服务器
  2. SQL Server Management Studio (SSMS) SQL Server管理Studio(SSMS)
  3. Adventure Works 2014 Full and Adventure Works DW 2014 databases Adventure Works 2014 Full和Adventure Works DW 2014数据库

入门 (Getting started)

1.产生一百万个姓氏和名字 (1. Generate a million first and last names)

In the first example, we will use the DimCustomer table from the AdventureWorksDW database mentioned in the requirements. This table contains 18,000 rows. We will use a cross join to generate all the possible combinations of names and last names. With the cross join you can generate a total combination of 341,658,256 users for your tests. The following example shows how to create a combination of 1 million user names and last names:

在第一个示例中,我们将使用需求中提到的AdventureWorksDW数据库中的DimCustomer表。 该表包含18,000行。 我们将使用交叉联接生成名称和姓氏的所有可能组合。 通过交叉联接,您可以为您的测试生成总共341,658,256个用户的组合。 以下示例显示了如何创建一百万个用户名和姓氏的组合:

se
USE [AdventureWorksDW2014]
GO
--Change 1000000 to the number of your preference for your needs
SELECT TOP 1000000c1.[FirstName],c2.[LastName]FROM [dbo].[DimCustomer] c1
CROSS JOIN
DimCustomer c2

The example will show 1,000,000 rows of names and last names:

该示例将显示1,000,000行名称和姓氏:

If you want to generate 34 million rows, you have to replace this line:

如果要生成3400万行,则必须替换以下行:


SELECT TOP 1000000

With this one:

有了这个:


SELECT TOP 34000000

The query generates a Cartesian product with all the combinations and TOP limits the number of rows.

该查询将生成具有所有组合的笛卡尔乘积,并且TOP限制行数。

2.生成随机整数值 (2. Generate random integer values)

The following example will show how to create a table of 1000 rows with random values from 1 to 100. We will use the RAND function to create random values and CHECKSUM(NEWID()) to generate distinct values. We use the cast to convert the values from real to integer:

下面的示例将说明如何创建一个包含1000行的表,其随机值介于1到100之间。我们将使用RAND函数创建随机值,并使用CHECKSUM(NEWID())生成不同的值。 我们使用强制转换将值从实数转换为整数:


with randowvaluesas(select 1 id, CAST(RAND(CHECKSUM(NEWID()))*100 as int) randomnumber--select 1 id, RAND(CHECKSUM(NEWID()))*100 randomnumberunion  allselect id + 1, CAST(RAND(CHECKSUM(NEWID()))*100 as int)  randomnumber--select id + 1, RAND(CHECKSUM(NEWID()))*100  randomnumberfrom randowvalueswhere id < 1000)select *from randowvaluesOPTION(MAXRECURSION 0)

The code will show 100 values between 1 to 100:

该代码将显示1到100之间的100个值:

If you want to generate 10000 values, change this line:

如果要生成10000个值,请更改此行:

id < 1000

编号<1000

With this one:

有了这个:

id < 10000

id <10000

If you want to generate values from 1 to 10000 change these lines:

如果要生成1到10000之间的值,请更改以下行:


select 1 id, CAST(RAND(CHECKSUM(NEWID()))*10000 as int) randomnumber
union  all
select id + 1, CAST(RAND(CHECKSUM(NEWID()))*10000 as int)  randomnumber
from randowvalues

If you want to generate real values instead of integer values use these lines replace these lines of the code displayed before:

如果要生成实数值而不是整数值,请使用这些行替换之前显示的代码的以下行:


select 1 id, CAST(RAND(CHECKSUM(NEWID()))*10000 as int) randomnumber
union  all
select id + 1, CAST(RAND(CHECKSUM(NEWID()))*10000 as int)  randomnumber
from randowvalues

And use these ones:

并使用这些:


select 1 id, RAND(CHECKSUM(NEWID()))*10000 randomnumber
union  allselect id + 1, RAND(CHECKSUM(NEWID()))*10000  randomnumber
from randowvalues

The query will show real numbers from 0 to 100

查询将显示从0到100的实数

3.特定范围内的随机实数 (3. Random real numbers with a specific range)

Another typical request is to provide random values with specific ranges. The following example will show a range of temperatures in °F (I really prefer the metric system, but I will do an exception this time).

另一个典型的要求是提供具有特定范围的随机值。 以下示例将显示以°F为单位的温度范围(我确实更喜欢公制系统,但是这次我会做一个例外)。

The human body has the following fluctuations of temperature: 95 to 105.8 °F (Normal temperature is from 97.7–99.5 °F, higher values means fever, Hyperthermia and lower values Hypothermia).

人体的温度波动如下:95至105.8°F(正常温度为97.7–99.5°F,较高的温度表示发烧,体温过高,较低的体温过低)。

In this example, we will generate values between 95 to 105.8 °F:

在此示例中,我们将生成介于95至105.8°F之间的值:


with randowvaluesas(--10.8 is the difference between 105.8 minus 95select 1 id,CAST(RAND(CHECKSUM(NEWID()))*10.8 as real) +95 as randomnumberunion  allselect id + 1,CAST(RAND(CHECKSUM(NEWID()))*10.8 as real)  +95 as randomnumberfrom randowvalueswhere id < 100)select *from randowvaluesOPTION(MAXRECURSION 0)

The result of the T-SQL statement will be values from 95 to 105.8 °F:

T-SQL语句的结果将是从95到105.8°F的值:

If you want real numbers from 6 to 10, change these lines of code:

如果要将实数从6改为10,请更改以下代码行:


select 1 id,CAST(RAND(CHECKSUM(NEWID()))*10.8 as real) +95 as randomnumberunion  all
select id + 1,CAST(RAND(CHECKSUM(NEWID()))*10.8 as real)  +95 as randomnumber

With these ones:

这些:


select 1 id,CAST(RAND(CHECKSUM(NEWID()))*4 as real) +6 as randomnumberunion  all
select id + 1,CAST(RAND(CHECKSUM(NEWID()))*4 as real)  +6 as randomnumber

Where 6 is the minimum value and 4 is the difference between 10 and 6.

其中最小值6是最小值,而4是10和6之间的差。

4. SQL Server中的随机密码 (4. Random passwords in SQL Server)

Another common request is to generate passwords. This example is used for initial passwords that will be changed latter by the user or when the user forgets the password.

另一个常见的请求是生成密码。 此示例用于初始密码,该初始密码将由用户在以后或用户忘记密码时进行更改。

The following example will generate 100 passwords:

以下示例将生成100个密码:


with randowvaluesas(select 1 id, CONVERT(varchar(20), CRYPT_GEN_RANDOM(10)) as mypasswordunion  allselect id + 1,  CONVERT(varchar(20), CRYPT_GEN_RANDOM(10)) as mypasswordfrom randowvalueswhere id < 100)select *from randowvaluesOPTION(MAXRECURSION 0)

The values displayed by the T-SQL statements are the following:

T-SQL语句显示的值如下:

We use the CRYPT_GEN_RANDOM function to generate passwords and we will then convert them to a varchar. The function returns hexadecimal values and we convert it to characters.

我们使用CRYPT_GEN_RANDOM函数生成密码,然后将其转换为varchar。 该函数返回十六进制值,然后将其转换为字符。

5.生成随机电子邮件 (5. Generating random emails)

The following example, will generate some passwords. We will use the First names and last names of the example 1 of the table DimCustomer to generate random fake emails in SQL Server. If we have for example a Customer named John Smith, we will generate an email that can be jsmith@gmail.com, or use a Hotmail or Yahoo account. Let’s take a look to the code:

下面的示例将生成一些密码。 我们将使用表DimCustomer的示例1的名字和姓氏在SQL Server中生成随机的伪造电子邮件。 例如,如果我们有一个名为John Smith的客户,我们将生成一封电子邮件,可以是jsmith@gmail.com,也可以使用Hotmail或Yahoo帐户。 让我们看一下代码:


USE [AdventureWorksDW2014]
GO
WITH random
as
(
SELECT TOP 10000c1.[FirstName],c2.[LastName],CAST(RAND(CHECKSUM(NEWID()))*3 as int) randomemailFROM [dbo].[DimCustomer] c1
CROSS JOIN
DimCustomer c2
)
select
Firstname,
Lastname,
email=
CASEwhen randomemail =0 then lower(left(FirstName,1)+[LastName])+'@hotmail.com'when randomemail =1 then lower(left(FirstName,1)+[LastName])+'@gmail.com'elselower(left(FirstName,1)+[LastName])+'@yahoo.com'
END
from random

The code will extract the first letter of the Firstname and concatenate with the last name and concatenate Hotmail or gmail or yahoo randomly:

该代码将提取“名字”的第一个字母,并与姓氏连接,并随机连接Hotmail或gmail或yahoo:

6.随机生成国名 (6. Generate country names randomly)

This last example will show how to generate random country names. We will use the table Person.CounryRegion from the adventureworks database and we will add an id using the Row_number function:

最后一个示例将显示如何生成随机国家/地区名称。 我们将使用Adventureworks数据库中的表Person.CounryRegion,并使用Row_number函数添加一个ID:


SELECT ROW_NUMBER() OVER(ORDER BY Name) AS id,[Name]FROM [AdventureWorks2016CTP3].[Person].[CountryRegion]

This table contains 238 countries:

下表包含238个国家:

We will use the list of random numbers of the second example to generate values from 1 to 238 (238 is the total number of countries) we will use an inner join to join the random numbers with the countries and generate country names randomly:

我们将使用第二个示例的随机数列表生成1到238之间的值(238是国家总数),我们将使用内部联接将随机数与国家/地区连接起来并随机生成国家/地区名称:


;with countriesas(
-- Create a country id and a country name. The countryid will be used to ---join with the random numbersSELECT ROW_NUMBER() OVER(ORDER BY Name) AS countryid,[Name]FROM [AdventureWorks2016CTP3].[Person].[CountryRegion]),
---Create 1000 random numbers from 1 to 238randowvaluesas(select 1 id, CAST(RAND(CHECKSUM(NEWID()))*238 as int) randomnumberunion  allselect id + 1, CAST(RAND(CHECKSUM(NEWID()))*238 as int)  randomnumberfrom randowvalueswhere id < 1000)--- Join countries with random numbers to generate country names randomlyselect randomnumber,c.Namefrom randowvalues rinner join countries con r.randomnumber=c.countryidorder by idOPTION(MAXRECURSION 0)

The T-SQL statements will generate a list of countries randomly:

T-SQL语句将随机生成国家列表:

结论 (Conclusions)

Generate random values for testing can be difficult. However, this article can be useful to inspire you to create your own data. Sometimes we can use existing tables to generate more values. Sometimes we can create the data from zero. In this example, we show how to create data using the Random function.

生成用于测试的随机值可能很困难。 但是,本文对于激发您创建自己的数据很有用。 有时我们可以使用现有表来生成更多值。 有时我们可以从零开始创建数据。 在此示例中,我们显示了如何使用随机函数创建数据。

In this article, we generated millions of first names and last names, random integer values, real values with specific ranges, random passwords, random emails using first and last names and random country names.

在本文中,我们生成了数百万个名字和姓氏,随机整数值,具有特定范围的实数值,随机密码,使用名字和姓氏以及随机国家/地区名称的随机电子邮件。

翻译自: https://www.sqlshack.com/how-to-generate-random-sql-server-test-data-using-t-sql/

如何使用T-SQL生成随机SQL Server测试数据相关推荐

  1. 深入理解 LINQ to SQL 生成的 SQL 语句

    Ø  简介 在 C# 中与数据交互最常用的语句就是 LINQ 了,而 LINQ to SQL 是最直接与数据库打交道的语句,它可以根据 LINQ 语法生成对应的 SQL 语句,在数据库中去执行.本文主 ...

  2. pg数据库生成随机时间_postgresql 时区与时间函数-阿里云开发者社区

    postgresql 时区与时间函数 rudygao 2016-02-03 1951浏览量 简介: --把时间戳转成epoch值 postgres=# select extract(epoch fro ...

  3. Visual Studio 2010生成SQL Server测试数据

    1.创建SQL Server 2005数据库项目 首先,打开Visual Studio 2010,选择新建项目,在下图中,选择建立一个SQL Server 2005的数据库项目,命名为Database ...

  4. sqlserver 人名_mssql sqlserver 使用sql脚本生成随机中文名字的方法分享

    摘要: 下文使用sql脚本生成中文名字的方法分享,如下所示: 实验环境:sql server 2008 R2 在工作中,我们有时需要批量生成随机姓名,下面将讲述使用sql脚本生成随机"名字& ...

  5. Entity Framewrok 7beta7中不同版本sql server自动生成分页sql语句的问题

    在EF中,使用linq进行分页是很方便的,假如我们有一个EMP表,结构如下: public class Emp{[Key]public Guid No { get; set; }public int ...

  6. java指定sql生成xml_SQL Server根据查询结果,生成XML文件

    /* 'bcp' 不是内部或外部命令,也不是可运行的程序? 看看在C:\Program Files\Microsoft SQL Server\80\Tools\Binn里面有没有bcp.exe这个文件 ...

  7. sql 生成csv数据_创建包含SQL Server数据的动态生成的CSV文件

    sql 生成csv数据 介绍 ( Introduction ) A few months back, I presented a paper at SQL Saturday 327 in Johann ...

  8. mysql 随机六位数_用SQL怎么写一个生成随机的六位数?

    展开全部 用SQL写一个生成随机的六位数代码如下: declare @i int set @i=0 while @i<1073 begin update Actor set AtrPwd = R ...

  9. sql tempdb清理_SQL Server TempDB数据库和闩锁争用

    sql tempdb清理 In this article, we will learn latch contention issues that we might experience in the ...

最新文章

  1. mysql 分号 存储过程_MySql 存储过程
  2. AI一分钟 | 小米MIX 2S将于3月27号发布,搭载骁龙845;张朝阳:在研究区块链 但相信AI的力量
  3. 一个平台系统架构师的能力模型是啥
  4. CVPR 2021 | 基于帧场学习的多边形建筑提取
  5. 说一说安装sklearn遇到的坑
  6. vb6 打印选项对话框_办公必备技能,Word打印问题及解决方案全在这,轻松解决打印难题...
  7. java ssh客户端_简单的Java SSH客户端
  8. 51Nod 1105 第K大的数 二分答案
  9. oracle 表达式1000,oracle环境下占用编号的方法报语法错误:ORA-01795: 列表中的最大表达式数为 1000...
  10. linux_ls命令详解
  11. 不用担心越界,不用中间变量的数值交换
  12. 如何将access数据库导入到sql2005数据库中
  13. Activity not started, its current task has been brought to the front 。
  14. xp系统简单tcpip服务器,XP系统怎样安装TCP/IP协议
  15. 重庆理工大学控制工程matlab大作业,(论文答辩)第六届校园数学建模知识竞赛决赛答辩会成功举行...
  16. android recyclerview监听滑动状态
  17. Linux服务器批量管理工具 - TeamRemote
  18. Linux - 操作系统
  19. 大学三年如何使用计算机论文,大学计算机导论论文3000字.docx
  20. 2023年全国最新会计专业技术资格精选真题及答案6

热门文章

  1. codevs 5958 无
  2. MySql大数据量恢复
  3. 查找路径php.ini文件到底在哪里?
  4. eclipse注释模板
  5. ★LeetCode(538)——把二叉搜索树转换为累加树(JavaScript)
  6. JavaScript学习(八十四)—变量
  7. 【百度地图】——利用三级联动加载百度地图
  8. CCF CSP201912-1 报数
  9. 理财里的封闭和半开放是啥意思?
  10. 之前8年都在上班工资16000,厌倦了天天上班的日子,就裸辞了。现在很迷茫,下一步怎么办?