Home Database Mysql Tutorial insert into select和select into的用法

insert into select和select into的用法

Jun 07, 2016 pm 02:53 PM
i insert select usage

insert into select和select into的用法 Insert是T-sql中常用语句,Insert INTO table(field1,field2,...) values(value1,value2,...)这种形式的在应用程序开发中必不可少。但我们在开发、测试过程中,经常会遇到需要表复制的情况,如将一个table1的数据的部


insert into select和select into的用法

 

Insert是T-sql中常用语句,Insert INTO table(field1,field2,...) values(value1,value2,...)这种形式的在应用程序开发中必不可少。但我们在开发、测试过程中,经常会遇到需要表复制的情况,如将一个table1的数据的部分字段复制到table2中,或者将整个table1复制到table2中,这时候我们就要使用SELECT INTO 和 INSERT INTO SELECT 表复制语句了。  www.2cto.com  

 

      1.INSERT INTO SELECT语句

 

      语句形式为:Insert into Table2(field1,field2,...) select value1,value2,... from Table1

 

      要求目标表Table2必须存在,由于目标表Table2已经存在,所以我们除了插入源表Table1的字段外,还可以插入常量。示例如下: --1.创建测试表

 

Sql代码  

create TABLE Table1  

(  

    a varchar(10),  

    b varchar(10),  

    c varchar(10),  

    CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED  

    (  

        a ASC  

    )  

) ON [PRIMARY]  

  

create TABLE Table2  

(  

    a varchar(10),  

    c varchar(10),  

    d int,  

    CONSTRAINT [PK_Table2] PRIMARY KEY CLUSTERED  

    (  

        a ASC  

    )  

) ON [PRIMARY]  

GO  

--2.创建测试数据  

Insert into Table1 values('赵','asds','90')  

Insert into Table1 values('钱','asds','100')  

Insert into Table1 values('孙','asds','80')  

Insert into Table1 values('李','asds',null)  

GO  

select * from Table2  

  

--3.INSERT INTO SELECT语句复制表数据  

Insert into Table2(a, c, d) select a,c,5 from Table1  

GO  

  

--4.显示更新后的结果  

select * from Table2  

GO  

--5.删除测试表  

drop TABLE Table1  

drop TABLE Table2  

    2.SELECT INTO FROM语句

      语句形式为:SELECT vale1, value2 into Table2 from Table1

 

      要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2中。示例如下:--1.创建测试表

 

Sql代码  

create TABLE Table1  

(  

    a varchar(10),  

    b varchar(10),  

    c varchar(10),  

    CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED  

    (  

        a ASC  

    )  

) ON [PRIMARY]  

GO  

  

--2.创建测试数据  

Insert into Table1 values('赵','asds','90')  

Insert into Table1 values('钱','asds','100')  

Insert into Table1 values('孙','asds','80')  

Insert into Table1 values('李','asds',null)  

GO  

  

--3.SELECT INTO FROM语句创建表Table2并复制数据  

select a,c INTO Table2 from Table1  

GO  

  

--4.显示更新后的结果  

select * from Table2  

GO  

--5.删除测试表  

drop TABLE Table1  

drop TABLE Table2  

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the best graphics card for i7 3770? What is the best graphics card for i7 3770? Dec 29, 2023 am 09:12 AM

What is the best graphics card for i7 3770?

Analyze the usage and classification of JSP comments Analyze the usage and classification of JSP comments Feb 01, 2024 am 08:01 AM

Analyze the usage and classification of JSP comments

How to correctly use the exit function in C language How to correctly use the exit function in C language Feb 18, 2024 pm 03:40 PM

How to correctly use the exit function in C language

Usage of WPSdatedif function Usage of WPSdatedif function Feb 20, 2024 pm 10:27 PM

Usage of WPSdatedif function

Asynchronous processing method of Select Channels Go concurrent programming using golang Asynchronous processing method of Select Channels Go concurrent programming using golang Sep 28, 2023 pm 05:27 PM

Asynchronous processing method of Select Channels Go concurrent programming using golang

Introduction to Python functions: Usage and examples of isinstance function Introduction to Python functions: Usage and examples of isinstance function Nov 04, 2023 pm 03:15 PM

Introduction to Python functions: Usage and examples of isinstance function

How to use Apple shortcuts How to use Apple shortcuts Feb 18, 2024 pm 05:22 PM

How to use Apple shortcuts

Detailed explanation and usage introduction of MySQL ISNULL function Detailed explanation and usage introduction of MySQL ISNULL function Mar 01, 2024 pm 05:24 PM

Detailed explanation and usage introduction of MySQL ISNULL function

See all articles