Tips | How to quickly create test data in SQL Server?

SQL column

Summary of basic knowledge of SQL database

Summary of advanced knowledge of SQL database

This article was first published on Knowledge Planet, join Knowledge Planet to learn more SQL tips

How do you create test data when doing exercises?

Is this the case, create a new table structure, then write the test data, and finally save it?

Today we will teach you a little trick to quickly create test data.

Why is it fast? Because he does not need to build a table structure, he can easily modify the data in the table at will.

This method has actually been said before, which is the table expression CTE

Yesterday, I asked my friend to get the second highest salary, and I did it manually. I feel it is necessary to teach this little skill to everyone.

Below is the test data I created.
WITH Employee AS(
SELECT 1 ID,100 Salary
UNION ALL
SELECT 2,200
UNION ALL
SELECT 3,300
)

SELECT * FROM Employee;

Note that the first line in the brackets must have the alias of each column, because it is used as the column name of each column.

In this way, there is a complete test data without the need to create a physical table, and the data inside can be easily added, modified or deleted.

This little trick can effectively improve the efficiency of usual code testing. Hope it can be useful to you.

By the way, this grammar is only supported by SQL Server and Oracle, and other platforms still have to create a table structure.

Guess you like

Origin blog.51cto.com/15057820/2655107