Select into from and insert to select from two table copy statement differences

Source: Internet
Author: User

Select into from and insert to select from two table copy statement differences

Select * into target_table from source_table;

Insert into target_table (column1,column2) select column1,5 from source_table;

The above two sentences are to insert the record of the source table source_table into the target table target_table, but the two sentences are different.
The first sentence (select to from) requires that the target table target_table not exist because it is created automatically when it is inserted.
The second sentence (insert to select from) requires that the target table target_table exist, because the target table already exists, so we can insert a constant in addition to inserting the field of the source table source_table, as in the example: 5.

For example, if you need to copy some of the field values from the User_city table to the Dim_pub_county_act table (with different field names), the statement is as follows:

INSERT
Into
SUI1. Dim_pub_county_act
(
CITY_ID,
COUNTY_ID,
Begin_time,
End_time,
County_name,
Active_flag,
Area_code,
Desc_txt
)
SELECT
ParentID as city_id,
Cityid as county_id,
' 2011-08-15 00:00:00 ' as Begin_time,
' 2011-08-15 00:00:00 ' as End_time,
CityName as County_name,
1 as Active_flag,
1 as Area_code,
' Area ' as Desc_txt
From
User_city
WHERE
ParentID like ' city1_ ';

-----------------------------------------------------------------

INSERT
Into
SUI1. Dim_pub_dept_act
(
DEPT_ID,
CITY_ID,
COUNTY_ID,
Dept_name,
Active_flag,
Begin_time,
End_time,
Desc_txt
)
SELECT
T1.cityid as dept_id,
T2.parentid as city_id,
T1.parentid as county_id,
T1.cityname as Dept_name,
1 as Active_flag,
' 2011-08-15 00:00:00 ' as Begin_time,
' 2011-08-15 00:00:00 ' as End_time,
' Area ' as Desc_txt
From
(SELECT * from user_city where parentid like ' city101_ ') t1, (SELECT * from user_city where parentid like ' city1_ ') t2

WHERE T1.parentid = T2.cityid;

Select into from and insert to select from two table copy statement differences

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.