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