SQL insert into select usage

Source: Internet
Author: User
Tags sql insert into select

Insert into select statement

Statement format: Insert into Table2 (field1, field2,...) select value1, value2,... from Table1

The target table Table2 must exist. Because the target table Table2 already exists, We can insert constants in addition to the fields in the source table 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 statement

Statement format: SELECT vale1, value2 into Table2 from Table1

The target table 2 does not exist because table 2 is automatically created during insertion and the specified field data in table 1 is copied to table 2. Example:


Select where with conditions

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 = '000000 ');

1 row created.

SQL>
SQL> select * from employee;

ID
------
NAME
----------------------------------------------------------------------
BIRTH_DATE GENDER
----------------------------------------
300
H
Null MALE

Insert 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.

Related Article

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.