最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
sql insert into select 的用法
时间:2022-06-29 10:10:43 编辑:袖梨 来源:一聚教程网
INSERT INTO SELECT语句
语句形式为:Insert into Table2(field1,field2,...) select value1,value2,... from Table1
要求目标表Table2必须存在,由于目标表Table2已经存在,所以我们除了插入源表Table1的字段外,还可以插入常量
SQL>
SQL>
SQL> CREATE TABLE employee (
2 id number,
3 name varchar(100),
4 birth_date date,
5 gender varchar2(30) );Table created.
SQL>
SQL>
SQL> INSERT INTO employee (id,name,birth_date,gender )
2 SELECT 200,'Chris',NULL,'MALE' from DUAL;1 row created.
SQL>
SQL>
SQL> drop table employee;Table dropped.
SELECT INTO FROM语句
语句形式为:SELECT vale1, value2 into Table2 from Table1
要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2中。示例如下:
带有条件选择where
SQL>
SQL> CREATE TABLE employee (
2 id number,
3 name varchar(100),
4 birth_date date,
5 gender varchar2(30) );Table created.
SQL>
SQL> INSERT INTO employee (id,name,birth_date,gender ) SELECT 300,'H',NULL,'MALE' from dual d
2 where not exists (SELECT 1 FROM employee x WHERE x.id = '300' );1 row created.
SQL>
SQL> select * from employee;ID
------
NAME
----------------------------------------------------------------------
BIRTH_DATE GENDER
---------- ------------------------------
300
H
null MALEInsert bulk by insert ... into ... select
SQL>
SQL> CREATE TABLE project (
2 pro_id NUMBER(4),
3 pro_name VARCHAR2(40),
4 budget NUMBER(9,2),
5 CONSTRAINT project_pk PRIMARY KEY (pro_id)
6 );Table created.
SQL>
SQL>
SQL> INSERT INTO project(pro_id, pro_name, budget)VALUES (1001, 'A',12345);1 row created.
SQL> INSERT INTO project(pro_id, pro_name, budget)VALUES (1002, 'ERP',23456);
1 row created.
SQL> INSERT INTO project(pro_id, pro_name, budget)VALUES (1003, 'SQL',34567);
1 row created.
SQL> INSERT INTO project(pro_id, pro_name, budget)VALUES (1004, 'CRM',45678);
1 row created.
SQL> INSERT INTO project(pro_id, pro_name, budget)VALUES (1005, 'VPN',56789);
1 row created.
SQL>
SQL>
SQL> SET ECHO ON
SQL> INSERT INTO PROJECT (pro_id, pro_name)
2 SELECT pro_id+8000,
3 SUBSTR(pro_name,1,31) || ' Overhead'
4 FROM project;5 rows created.
相关文章
- 云星穹铁道权益升级怎么玩 云崩铁权益升级活动介绍 12-03
- 以闪亮之名山中绘梦怎么玩 以闪亮之名滢然花梦套装活动介绍 12-03
- 以闪亮之名绿野梦歌礼包怎么样 以闪亮之名绿野梦歌拍照礼包介绍 12-03
- 光遇12.6有什么更新 光遇12月6日更新内容介绍 12-03
- 伍六七暗影交锋测试资格怎么获得 伍六七暗影交锋内测资格获取方法 12-03
- 无限暖暖晶莹诗集怎么样 12-03