Today, when we are working on a project, we suddenly need to insert multiple records into the database, but these records must be inserted into the database using an insert statement. At this time, you will say that you can use insert
Xxx select XXX sentence, but what is even more confusing is that all data to be inserted is constant, that is, there is no table that can be selected into multiple rows!
Instead, we need to convert some constants into multiple rows or multiple records. How can this problem be solved?
I know that multiple constants can be combined into one row in the form of select 1, 2, 3, but how can I combine multiple constants into multiple rows?
Baidu knows that a senior man seems to have encountered my problem in advance:
Http://zhidao.baidu.com/question/231435615.html
Therefore, the following method is available:
Select * from (select 153 Union select 154 Union select 155) T (OID)
The preceding statement returns a table named T, which has only one column named oid. The table contains three rows of records, which are 153,154,155:
Table t
Oid
____
| 1, 153 |
____
| 1, 154 |
____
| 1, 155 |
____
What should you do next?
Insert into XXX select XXX from (select 153 Union select 154 Union select 155) T (OID)
You can use one SQL statement to insert multiple rows.