No temporary table is used. Only select is used to query multiple row constants.

Source: Internet
Author: User

 

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.

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.