Select INTO and INSERT into select two table copy statements. txt

Source: Internet
Author: User

Insert is a common statement in T-SQL, insert into table (Field1,field2,...) VALUES (value1,value2,...) This form is essential in application development. However, during the development and testing process, we often encounter situations that require table duplication, such as copying a partial field of a Table1 data to table2, or copying the entire table1 to table2, when we are going to use SELECT INTO and INSERT into The SELECT table copies the statement. The 1.INSERT into SELECT statement is in the form: INSERT into Table2 (field1,field2,...) Select Value1,value2,... the from Table1 requires that the target table Table2 be present, Since the target table Table2 already exists, we can insert constants in addition to the fields Table1 the source table. Examples are: Copy code--1. Creating a Test Table CREATE TABLE Table1 (a varchar), B varchar (ten), C varchar (ten), CONSTRAINT [Pk_table1] PRIMARY KEY CLUSTERED (a ASC)) on [PRIMARY] Create TABLE Table2 (a varchar (ten), C varchar (ten), d int, CONSTRAINT [pk_table2] PRIMA RY KEY CLUSTERED (a ASC)) on [PRIMARY] GO--2. Creating test data INSERT into Table1 values (' Zhao ', ' ASDs ', ' all ') insert into Table1 valu Es (' Money ', ' ASDs ', ' + ') insert into Table1 values (' Sun ', ' ASDs ', ' + ') insert into Table1 values (' Lee ', ' ASDs ', null) GO SELECT * Fro M Table2--3.insert into SELECT statement Copy table Data INSERT into Table2 (A, C, D) Select a,c,5 from Table1 GO--4. Show updated Results select * FROM Table2 GO--5. Delete Test table drop table Table1 drop table Table2 Copy code 2.SELECT into from statement form: SELECT vale1, value2 into Table2 F Rom Table1 requires that the target table Table2 not exist because the table Table2 is automatically created on insert and the specified field data in Table1 is copied to Table2. Examples are: Copy code--1. Creating a Test Table CREATE TABLE Table1 (a varchar), B varchar (ten), C varchar (ten), CONSTRAINT [Pk_table1] PRIMARY KEY CLUSTERED (a ASC)) on [PRIMARY] GO--2. Create test data insert into Table1 values (' Zhao ', ' ASDs ', ' all ') insert into Table1 values (' money ', ' ASDs ', ' + ') insert into Table1 values (' Sun ', ' ASDs ', ' + ') insert into Table1 values (' Lee ', ' ASDs ', null) GO--3.select into FRO The M statement creates the table Table2 and copies the data select A,c to Table2 from Table1 go--4. Show updated Results select * from Table2 GO--5. Delete Test table drop tables Table1 D ROP TABLE Table2 Copy Code

Select INTO and INSERT into SELECT two table copy statements. txt

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.