Select INTO and insert into Select two table copy statement _mssql

Source: Internet
Author: User

The first sentence: select * into [ToTable] the second sentence of [fromtable]
: INSERT into [ToTable] ([fild_one],[fild_two]) SELECT [Fild_one], 8 from [fromtable]

Both of the above sentences insert [fromtable] data into [ToTable], but the two are different:

The first sentence (SELECT into from) requires the target table [ToTable] Not exists because it is created automatically when inserting. The
second sentence (insert into SELECT from) requires the target table [ToTable] to exist, and because the target table already exists, we can insert a constant, in addition to the fields in the source table [Fromtable], as in the example: 8.

Insert is a common statement in T-SQL, insert into table (Field1,field2,...) VALUES (value1,value2,...) This form is essential in application development. But in our development, testing process, we often encounter situations that require table replication, such as copying part of a Table1 data to table2, or copying the entire table1 to table2, when we use SELECT INTO and INSERT into The SELECT table replicates the statement. The
1.INSERT into SELECT statement
statement is: INSERT into Table2 (field1,field2,...) Select Value1,value2,... from Table1
Requirements The Table2 must exist, because the target table Table2 already exists, so we can insert a constant in addition to the fields in the source table Table1. Examples are as follows:

--1. Create a test table 
Table1 
( 
a varchar), B varchar (a), varchar CONSTRAINT 
[pk_ Table1] PRIMARY KEY CLUSTERED 
( 
a ASC 
) 
) on [PRIMARY] 
Create TABLE Table2 
( 
a varchar ( 
(a), C varchar (ten), 
D int, 
CONSTRAINT [pk_table2] PRIMARY KEY CLUSTERED 
( 
a ASC 
) 
On [PRIMARY] go 
--2. Create test data 
insert into Table1 values (' Zhao ', ' ASDs ', ', ') 
insert into Table1 values ( ' Money ', ' ASDs ', ', ' 
insert into Table1 values (' Sun ', ' ASDs ', ') 
insert into Table1 values (' Lee ', ' ASDs ', null) 
Go 
select * from Table2 
--3.insert into SELECT statement Copy table Data 
INSERT into Table2 (A, C, D) select a,c,5 from T Able1 
go 
--4. Show updated Results 
select * from Table2 go 
--5. Delete Test table 
drop table Table1 
drop TABLE Table2 


2.SELECT into from statement

Statement form: SELECT vale1, value2 into Table2 from Table1
The target table Table2 does not exist because the table Table2 is created automatically at insert time and the specified field data in Table1 is copied to Table2. Examples are as follows:

--1. Creating a Test Table CREATE TABLE Table1 (a varchar (a), B varchar (a), C varchar (a), constr aint [pk_table1] PRIMARY KEY CLUSTERED (a ASC)) on [PRIMARY] go--2. Create test data Insert into Table1 values (' Zhao ', ' ASDs ', INSERT into Table1 values (' money ', ' ASDs ', ') insert into Table1 values (' Sun ', ' ASDs ', ') insert into Table1 values (  ' Lee ', ' ASDs ', null ' go--3.select into the FROM statement to create a table Table2 and copy the data select A,c into Table2 to go Table1. Show updated Results SELECT * From Table2 go--5. Delete Test table drop table Table1 drop table Table2 

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.