SELECT INTO & INSERT INTO SELECT

前言

本文讲解如何实现表复制语句;

INSERT INTO SELECT语句

  • 使用场景,将表1的查询结果复制到表2中去;
  • 语法:Insert into Table2(field1,field2,...) select value1,value2,... from Table1
--1.创建测试表
    create TABLE Table1
    (
        a varchar(10),
        b varchar(10),
        c varchar(10)
    ) 

    create TABLE Table2
    (
        a varchar(10),
        c varchar(10),
        d int
    ) 
    --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);
 
    select * from Table2

    --3.INSERT INTO SELECT语句复制表数据
    Insert into Table2(a, c, d) select a,c,5 from Table1


    --4.显示更新后的结果
    select * from Table2

    --5.删除测试表
    drop TABLE Table1
    drop TABLE Table2

你可能感兴趣的:(SELECT INTO & INSERT INTO SELECT)