Several ways for DB2 to write data to a table

Source: Internet
Author: User

DB2 writes data to a table using the following methods: 1. insert into table values (...) write data according to the specified column in the table. If only the table name is given, the data is added in the column order. Insert into table (col1, col2) values (value1, value2): insert into table (col1, col2) values (value1, value2), (value3, value4 ),... 2. insert into table select... this statement indicates that the added data comes from one or more tables or attempts to query. The query can be sorted and grouped. For example, the create table empk (empno char (6), ename varchar (15), and salary decimal (9, 2) DB20000I SQL command is successfully completed. The three employees with the highest salary: db2 => insert into empk select empno, lastname, salary from employee order by salary desc fetch first 3 rows onlyDB20000I SQL command is successfully completed. Db2 => select * from empkEMPNO ename salary ------ ------------- ----------- 000010 HAAS 152750.00000030 KWAN 98250.00000070 PULASKI 96170.00 3 records are selected. 3. Copy the table structure and data. We can also copy the table structure when creating the table, and then use this method to add data. Create table emp_test like employeeinsert into emp_test select * from employee where salary> 80000.00 you can also copy data while copying the table structure. Create table emp_test as (select * from employee where salary> 80000.00) data initially deferred refresh immediate | after the deferred table is created, refresh it: db2 => the refresh table emp_testDB20000I SQL command is successfully completed. Otherwise, an error occurs: SQL0668N cannot perform operations on the table "LENOVO. EMP_TEST". The Reason code is "1 ". SQLSTATE = 57016 if you only want to copy the table structure and do not need to copy data, you can create a table as follows: create table emp_tt as (select empno, job, salary from employee) definition only --★Or create table emp_tt as (select empno, job, salary from employee) with no data --★Db2 => describe table emp_tt data type column name mode data type name long DECIMAL places NULL ------------------------- ---------- ------ empno sysibm character 6 0 no job sysibm character 8 0 is salary sysibm decimal 9 2 yes. Three records have been selected. Db2 => select count (*) from emp_tt1 ----------- 0 1 record selected. 4. The Merge into statement is used to merge the data in one table into another. When the data already exists, we do not need to process or update it. Source: http://blog.csdn.net/bobo12082119/article/details/8780814 -- The end --

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.