search
Homephp教程php手册[MSSQL]SELECT INTO 和 INSERT INTO SELECT 两种表复制语句

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

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

      1.INSERT INTO SELECT语句

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

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

INSERT INTO SELECT语句复制表数据
   
--1.创建测试表
    createTABLE Table1
     (
         a
varchar(10),
         b
varchar(10),
         c
varchar(10),
        
CONSTRAINT[PK_Table1]PRIMARYKEYCLUSTERED
         (
             a
ASC
         )
     )
ON[PRIMARY]

    
createTABLE Table2
     (
         a
varchar(10),
         c
varchar(10),
         d
int,
        
CONSTRAINT[PK_Table2]PRIMARYKEYCLUSTERED
         (
             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 INTOSELECT语句复制表数据
    Insert into Table2(a, c, d) select a,c,5from Table1
    
GO

    
--4.显示更新后的结果
    select *from Table2
    
GO
    
--5.删除测试表
    dropTABLE Table1
    
dropTABLE Table2


      2.SELECT INTO FROM
语句

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

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

SELECT INTO FROM创建表并复制表数据

Statement
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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software