700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 如何使用T-SQL生成随机SQL Server测试数据

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

时间:2019-07-01 06:56:18

相关推荐

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

介绍 (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中生成多达一百万行的随机数据,包括:

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

要求 (Requirements)

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

入门 (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个用户的组合。 以下示例显示了如何创建一百万个用户名和姓氏的组合:

seUSE [AdventureWorksDW]GO--Change 1000000 to the number of your preference for your needsSELECT TOP 1000000c1.[FirstName],c2.[LastName]FROM [dbo].[DimCustomer] c1CROSS JOINDimCustomer 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 randomnumberunionallselect id + 1, CAST(RAND(CHECKSUM(NEWID()))*100 as int)randomnumber--select id + 1, RAND(CHECKSUM(NEWID()))*100randomnumberfrom 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) randomnumberunionallselect id + 1, CAST(RAND(CHECKSUM(NEWID()))*10000 as int)randomnumberfrom 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) randomnumberunionallselect id + 1, CAST(RAND(CHECKSUM(NEWID()))*10000 as int)randomnumberfrom randowvalues

And use these ones:

并使用这些:

select 1 id, RAND(CHECKSUM(NEWID()))*10000 randomnumberunionallselect id + 1, RAND(CHECKSUM(NEWID()))*10000randomnumberfrom 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 randomnumberunionallselect 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 randomnumberunionallselect 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 randomnumberunionallselect 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 mypasswordunionallselect 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@, or use a Hotmail or Yahoo account. Let’s take a look to the code:

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

USE [AdventureWorksDW]GOWITH randomas(SELECT TOP 10000c1.[FirstName],c2.[LastName],CAST(RAND(CHECKSUM(NEWID()))*3 as int) randomemailFROM [dbo].[DimCustomer] c1CROSS JOINDimCustomer c2)selectFirstname, Lastname,email=CASEwhen randomemail =0 then lower(left(FirstName,1)+[LastName])+'@'when randomemail =1 then lower(left(FirstName,1)+[LastName])+'@'elselower(left(FirstName,1)+[LastName])+'@'ENDfrom 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 [AdventureWorksCTP3].[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 [AdventureWorksCTP3].[Person].[CountryRegion]),---Create 1000 random numbers from 1 to 238randowvaluesas(select 1 id, CAST(RAND(CHECKSUM(NEWID()))*238 as int) randomnumberunionallselect 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.

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

翻译自: /how-to-generate-random-sql-server-test-data-using-t-sql/

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。