[sql server] SELECT INTO 和 INSERT INTO SELECT

INSERT INTO table name VALUES (value 1, value 2, ....) - single data inserted, must have a full field value
INSERT INTO table_name (column 1, column 2, ...) VALUES (value 1, value 2 , ....) - inserted into a single data field portion, the value portion

INSERT INTO table_name (column 1, column 2, ...) the VALUES
(value 1, value 2, ....),
(value 1, value 2, ....),
(value 1, value 2, .. ..),
(value 1, value 2, ....)
- insertion of multiple statements

1.INSERT INTO SELECT statement

Statement in the form of: Insert into Table2 (field1, field2, ...) select value1, value2, ... from Table1

或者:Insert into Table2 select  *  from Table1

Note: (1) requires certain Table2 table must exist , and the field field, field2 ... must be present

(2) Note that the primary key constraint Table2, and if Table2 primary key is not empty, then field1, field2 ... must include a primary key

(3) Note that the syntax, do not add values, and insert a data sql mixed, instead of writing:

The first , to be inserted when the table (Student_back) does not exist
- the Student table gender inserted as 'M' to a student information table Student_back (does not exist) in the
select * into Student_back from Student where S_Sex = ' M'

specific column is not specified, it will automatically create a heel (Student) the same table and insert the data
to assign specific columns you want to insert a table is created according to the specified column and insert data

2.SELECT INTO FROM statement

Statement form: SELECT vale1, value2 into Table2 from Table1

Requires certain Table2 table does not exist , because the table is automatically created upon insertion of Table2, and replication fields specified in Table1 data in Table2.

Second , when you want to insert the table itself will exist
- in the table Student gender 'female' student information into a table Student_back (exist) in
INSERT INTO Student_back (S_StuNo, s_name, S_Sex, S_Height) (new table )
SELECT S_StuNo, s_name, S_Sex, S_Height from Student (old table)
wHERE S_Sex = 'M'

as S_Id field is an identity column, there can be displayed the value inserted, it must be specified here insert column.
Excerpt: https: //blog.csdn.net/qq_21960819/article/details/88778116

https://www.cnblogs.com/piaoyangguoguo/p/8276928.html

 

 

Guess you like

Origin www.cnblogs.com/onionfly/p/11027768.html