`
weiruan85
  • 浏览: 385155 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

db2 常用 语句

    博客分类:
  • db2
阅读更多
将某个表导出为IXF档:

Sql代码
CONNECT TO CSI;  
EXPORT TO "C:\T_R_FORM.ixf" OF IXF MESSAGES "aa" SELECT * FROM CSIDDBD3.T_REFERRAL_FORM;  
CONNECT RESET;  
 
CONNECT TO CSI;  
EXPORT TO "C:\T_R_FORM_FLAG.ixf" OF IXF MESSAGES "bb"   
SELECT * FROM CSIDDBD3.T_REFERRAL_FORM_FLAG;  
CONNECT RESET; 

CONNECT TO CSI;
EXPORT TO "C:\T_R_FORM.ixf" OF IXF MESSAGES "aa" SELECT * FROM CSIDDBD3.T_REFERRAL_FORM;
CONNECT RESET;

CONNECT TO CSI;
EXPORT TO "C:\T_R_FORM_FLAG.ixf" OF IXF MESSAGES "bb"
SELECT * FROM CSIDDBD3.T_REFERRAL_FORM_FLAG;
CONNECT RESET;

2、日期类型(Date)作为查询条件:

Sql代码
SELECT * FROM CSIDDBD3.T_ID_MSGPOP WHERE BEG_DATE > '2007-01-01'; 

SELECT * FROM CSIDDBD3.T_ID_MSGPOP WHERE BEG_DATE > '2007-01-01';

3、时间类型(TimeStamp)作为查询条件:

Sql代码
SELECT * FROM CSIDDBD3.T_ID_MSGPOP WHERE INPUT_DATE > '2007-01-01 00:00:00'; 

SELECT * FROM CSIDDBD3.T_ID_MSGPOP WHERE INPUT_DATE > '2007-01-01 00:00:00';

4、取前N条记录

Sql代码
SELECT * FROM CSIDDBD3.T_ID_MSGPOP FETCH FIRST 1000 ROWS ONLY; 

SELECT * FROM CSIDDBD3.T_ID_MSGPOP FETCH FIRST 1000 ROWS ONLY;

5、如何利用表的NOT LOGGED INITIALLY特性清空表中的数据

Sql代码
alter  table table_name activate not logged initially with empty table  

alter  table table_name activate not logged initially with empty table 参考资料



6、批量更新某一查询结果集的第N条到第M条的记录的SQL(DB2)

Sql代码
update tableName b  
set b.A=2 ,b.B=current date  --B字段为日期类型  
where exists (  
   select 1 from (  
      select a.id,row_number()over(order by a.id) req --以tableName表的主键id字段进行排序  
         from tableName a where a.C =22 and a.D=1) c --加上查询结果集的限制条件  
  where b.id=c.id and c.req>=1 and c.req<=3);--将要更新查询结果集的第1到第3条记录 

update tableName b
set b.A=2 ,b.B=current date  --B字段为日期类型
where exists (
   select 1 from (
      select a.id,row_number()over(order by a.id) req --以tableName表的主键id字段进行排序
         from tableName a where a.C =22 and a.D=1) c --加上查询结果集的限制条件
  where b.id=c.id and c.req>=1 and c.req<=3);--将要更新查询结果集的第1到第3条记录

7、DB2 如何设置最大连接数?

Sql代码
db2 connect to dbname user username  using  passwd  
db2 update db cfg using MAXAPPLS  number 

db2 connect to dbname user username  using  passwd
db2 update db cfg using MAXAPPLS  number 
8、SUBSTR()函数的使用

函数原型:

Sql代码
SUBSTR(string-expression,start,length) 

SUBSTR(string-expression,start,length)【 示例】Sample table DSN8810.PROJ contains column PROJNAME, which is defined as VARCHAR(24). Select all rows from that table for which the string in PROJNAME begins with 'W L PROGRAM '.

Sql代码
SELECT * FROM DSN8810.PROJ WHERE SUBSTR(PROJNAME,1,12) = 'W L PROGRAM '; 

SELECT * FROM DSN8810.PROJ WHERE SUBSTR(PROJNAME,1,12) = 'W L PROGRAM ';

【示例2】DB2函数substr分析

错误SQL:

Java代码
select pk_invcl from bd_invcl   
where length (invclasscode) <= length ('501')   
and substr ('501', 1, length (invclasscode)) = invclasscode   
order by invclasscode 

select pk_invcl from bd_invcl
where length (invclasscode) <= length ('501')
and substr ('501', 1, length (invclasscode)) = invclasscode
order by invclasscode

错误日志:

Xml代码
2007-01-18 19:10:13 SO|java.sql.SQLException: [IBM][CLI Driver][DB2/AIX64] SQL0138N   
The second or third argument of the SUBSTR function is out of range. SQLSTATE=22011 

2007-01-18 19:10:13 SO|java.sql.SQLException: [IBM][CLI Driver][DB2/AIX64] SQL0138N
The second or third argument of the SUBSTR function is out of range. SQLSTATE=22011

分析:虽然第一个条件已经限制length (invclasscode) <= length ('501') ,但是不满足第一个条件时,DB2第二个条件还会执行。
但是以下SQL却可以执行,并且以下两个SQL返回结果相同

Sql代码
select count(*) from bd_invcl   
where substr (invclasscode, 1,length(invclasscode)+2) = invclasscode  
select count(*) from bd_invcl 

select count(*) from bd_invcl
where substr (invclasscode, 1,length(invclasscode)+2) = invclasscode
select count(*) from bd_invcl

修正后:

Sql代码
select pk_invcl from bd_invcl   
where length (invclasscode) < length ('501')   
and substr ('501', 1,   case   
                  when length (invclasscode)>length ('501')   
                  then length ('501')   
                  else length (invclasscode) end   )   
= invclasscode order by invclasscode 

select pk_invcl from bd_invcl
where length (invclasscode) < length ('501')
and substr ('501', 1,   case
                  when length (invclasscode)>length ('501')
                  then length ('501')
                  else length (invclasscode) end   )
= invclasscode order by invclasscode 
9、自动增长列

    A、包含生成列的 DB2 表上的数据移入和移出 — 入门

    B、在 DB2 通用数据库中自动生成数值序列

    C、DB2自动增长主键的方法。

    D、急,DB2 9中自动增长列如何返回(在线等)



《Understanding DB2® Learning Visually with Examples》



7.8.6. Identity Columns
An identity column is a numeric column in a table that automatically generates a unique numeric value in sequence for each row inserted. A unique identifier is often used in applications to identify a specific row. Unlike sequence objects, which we discuss in section 7.16 , Sequences, identity columns are bound to the table they are defined on. There can be only one identity column per table. DB2 can generate the identity column values in two ways.

Generated always : The values are always generated by DB2. Applications are not allowed to provide an explicit value.

Generated by default : The values can be explicitly provided by an application; if no value is given, DB2 generates one. In this case, however, DB2 cannot guarantee the uniqueness of the value generated.

To create an identity column, use the CREATE TABLE statement with the GENERATED clause and make sure it contains the IDENTITY keyword because GENERATED can also be used to generate other values automatically that are not identity columns. Here is an example.

Sql代码
CREATE TABLE product (  
       productno   INTEGER GENERATED ALWAYS AS 
                           IDENTITY (START WITH 200 INCREMENT BY 1),  
       description VARCHAR(50) )  

CREATE TABLE product (
       productno   INTEGER GENERATED ALWAYS AS
                           IDENTITY (START WITH 200 INCREMENT BY 1),
       description VARCHAR(50) ) 

The column productno is an INTEGER defined as an identity column that is always generated. The value generated will start from 200, and it will be incremented by 1. Let's perform a few INSERT statements and see the results obtained.


INSERT INTO product VALUES (DEFAULT,'banana');         --->inserts 200,banana
INSERT INTO product (description) VALUES ('apple');    --->inserts 201,apple
INSERT INTO product VALUES (300,'pear');               --->error SQL0798N
COMMIT;

INSERT INTO product (description) VALUES ('orange');   --->inserts 202,orange
ROLLBACK;

INSERT INTO product (description) VALUES ('plum');     --->inserts 203,plum
COMMIT ;

The following query shows the final result.

Sql代码
SELECT * FROM product;  
 
PRODUCTNO   DESCRIPTION  
----------- ------------  
        200 banana  
        201 apple  
        203 plum 

SELECT * FROM product;

PRODUCTNO   DESCRIPTION
----------- ------------
        200 banana
        201 apple
        203 plum

The first two INSERT statements show that two identity column values were generated: 200 and 201. The third INSERT statement returns an error because you cannot explicitly insert a value for an identity column generated as ALWAYS . After the third INSERT statement, we issue a COMMIT to guarantee these rows are stored in the database. The fourth INSERT statement causes another identity column value, 202, to be generated; however, we issue a ROLLBACK statement right after, so this row is not stored in the database. Note that the final INSERT statement, which inserts the product plum, generates a value of 203, not 202. (COMMIT and ROLLBACK statements are explained in more detail in Chapter 13 , Developing Database Backup and Recovery Solutions.)



NOTE

An identity column value is generated only once. Once the value has been generated, even if a ROLLBACK statement is performed, it will not be generated again.



Now let's review another example, this time creating the same table product with the GENERATED BY DEFAULT clause.

Sql代码
CREATE TABLE product (  
       productno   INTEGER GENERATED BY DEFAULT AS 
                           IDENTITY (START WITH 200 INCREMENT BY 1),  
       description VARCHAR(50) ) 

CREATE TABLE product (
       productno   INTEGER GENERATED BY DEFAULT AS
                           IDENTITY (START WITH 200 INCREMENT BY 1),
       description VARCHAR(50) ) 
Next, we insert a few rows.

Sql代码
INSERT INTO product VALUES (DEFAULT,'banana');         --->inserts 200,banana  
INSERT INTO product (description) VALUES ('apple');    --->inserts 201,apple  
INSERT INTO product VALUES (300,'pear');               --->inserts 300,pear  
INSERT INTO product VALUES (201,'orange');             --->inserts 201,orange  
COMMIT;  
INSERT INTO product (description) VALUES ('papaya');   --->inserts 202,papaya  
ROLLBACK;  
INSERT INTO product (description) VALUES ('plum');      --->inserts 203,plum  
COMMIT;  

INSERT INTO product VALUES (DEFAULT,'banana');         --->inserts 200,banana
INSERT INTO product (description) VALUES ('apple');    --->inserts 201,apple
INSERT INTO product VALUES (300,'pear');               --->inserts 300,pear
INSERT INTO product VALUES (201,'orange');             --->inserts 201,orange
COMMIT;
INSERT INTO product (description) VALUES ('papaya');   --->inserts 202,papaya
ROLLBACK;
INSERT INTO product (description) VALUES ('plum');      --->inserts 203,plum
COMMIT; 


The following query shows the final result.

Sql代码
SELECT * FROM product  
 
PRODUCTNO   DESCRIPTION  
----------- ---------------------  
        200 banana  
        201 apple  
        300 pear  
        201 orange  
        203 plum 

SELECT * FROM product

PRODUCTNO   DESCRIPTION
----------- ---------------------
        200 banana
        201 apple
        300 pear
        201 orange
        203 plum

The first two INSERT statements show that two identity column values were generated: 200 and 201. For the third and fourth INSERT statements, we explicitly provided the values 300 and 201, respectively, for the identity column. Note that DB2 did not return an error as in the previous example because we defined the identity column as GENERATED BY DEFAULT . After the fourth INSERT statement, we issue a COMMIT to guarantee these rows are stored in the database. The fifth INSERT statement causes another identity column value, 202, to be generated; however, we issue a ROLLBACK statement right after, so this row is not stored in the database. Note that the final INSERT statement, which inserts the product plum, generates a value of 203, not 202.

The following final example illustrates a GENERATED value, which is not an identity column. The example uses GENERATED ALWAYS , but you can also use GENERATED BY DEFAULT .

Sql代码
CREATE TABLE income (  
empno     INTEGER,  
salary    INTEGER,  
taxRate   DECIMAL(5,2),  
netSalary DECIMAL(7,2) GENERATED ALWAYS AS (salary * (1 - taxRate))  


CREATE TABLE income (
empno     INTEGER,
salary    INTEGER,
taxRate   DECIMAL(5,2),
netSalary DECIMAL(7,2) GENERATED ALWAYS AS (salary * (1 - taxRate))

If you insert the following row:

Sql代码
INSERT INTO income (empno, salary, taxRate) VALUES (111, 50000, 0.3)  

INSERT INTO income (empno, salary, taxRate) VALUES (111, 50000, 0.3) 

The result is:

Sql代码
EMPNO       SALARY      TAXRATE NETSALARY  
----------- ----------- ------- ---------  
        111       50000    0.30  35000.00 

EMPNO       SALARY      TAXRATE NETSALARY
----------- ----------- ------- ---------
        111       50000    0.30  35000.00

DB2 generates the value of the last column NETSALARY based on the SALARY and TAXRATE columns.
分享到:
评论

相关推荐

    DB2常用语句集萃

    【DB2常用语句集萃】中的知识点涵盖了SQL查询的多个方面,主要涉及了数据的检索、聚合、分组、转换以及联接操作。以下是对这些知识点的详细说明: 1. **空值处理**:使用`ISNULL`函数来处理NULL值,例如`isnull...

    db2常用语句

    本文将基于给定的“db2常用语句”标题、描述及部分内容,深入探讨DB2中的关键操作命令,涵盖数据操作、表结构修改、连接管理、性能监控等多个方面。 ### 1. 数据连接与断开 - **连接数据库**:`db2 connect to ...

    db2常用45条语句

    以下是一些关于DB2的常用SQL语句,它们涵盖了查询、聚合、条件过滤、转换等功能,对于理解和操作DB2数据库非常有帮助: 1. **查询员工信息**:这条语句通过`ISNULL`函数处理出生日期为空的情况,将其替换为“日期不...

    DB2常用语句

    ### DB2常用语句详解 #### 一、概述 IBM DB2是一种广泛使用的数据库管理系统,为企业提供了高效的数据存储与检索解决方案。为了更好地管理和优化DB2数据库的性能,掌握一系列常用的DB2语句变得尤为重要。本文将...

    db2常用句子

    根据提供的文件信息,我们可以归纳出一系列关于IBM DB2数据库管理系统的常用命令与操作知识点。以下是对这些知识点的详细解析: ### 1. 启动DB2实例 **命令**: `#db2start` - **作用**: 用于启动DB2数据库实例。 - ...

    DB2常用的命令

    ### DB2常用的命令详解 #### 一、数据库的启动与停止 - **启动数据库**: `db2start` - **作用**: 启动指定的DB2数据库实例。 - **示例**: 如果需要启动名为`oyd`的数据库实例,则执行`db2start`。 - **停止...

    DB2数据库SQL注入语句

    本文将深入探讨DB2数据库中的SQL注入语句,以及如何通过这些语句来猜解数据库结构和数据。 首先,SQL注入的基础原理是通过在合法的SQL查询语句中嵌入恶意代码,以改变原本的查询逻辑。在给定的示例中,攻击者试图猜...

    Db2 常用命令Db2 常用命令Db2 常用命令.doc

    ### Db2常用命令详解 #### 一、Db2概述与基本命令 Db2是由IBM公司开发的一款关系型数据库管理系统(RDBMS),广泛应用于企业级数据处理领域。掌握Db2的基本命令对于日常维护和管理至关重要。 - **启动DB2管理器**...

    DB2 常用技术参考.doc

    DB2是IBM公司开发的一款关系型数据库...这些DB2常用技术涵盖了数据库管理、查询优化和安全性等方面,对于DB2的日常管理和开发工作具有很高的参考价值。通过理解和掌握这些知识点,能够更有效地管理和操作DB2数据库。

    db2数据库sql语句大全

    根据提供的标题、描述以及部分文本内容,我们可以整理出与DB2数据库相关的多个知识点。...以上内容涵盖了DB2数据库中一些常用的操作命令和SQL语句,对于日常维护和管理DB2数据库具有重要的参考价值。

    linux下DB2常用命令

    Linux 下 DB2 常用命令 DB2 是一个 relation database management system(关系数据库管理系统),广泛应用于企业级的数据存储和管理中。在 Linux 环境下,DB2 提供了丰富的命令行工具,用于管理和维护数据库。下面...

    DB2常用函数(初学者必备)

    本文将深入探讨DB2中的常用函数,这些函数覆盖了类型转换、日期时间操作、字符串处理以及数学计算等多个方面,对于初学者来说,掌握这些函数能够极大地提升在DB2环境下的数据操作效率。 ### 类型转换函数 DB2提供...

    db2常用命令集合(经典语句集合)

    这份"db2常用命令集合(经典语句集合)"文档很显然是为了帮助用户更好地理解和掌握DB2的操作。接下来,我们将深入探讨其中的一些关键知识点。 1. **安装与启动**: - `db2start`:启动DB2实例,确保数据库服务正常...

    DB2常用SQL写法

    DB2常用SQL写法 以下是对DB2常用SQL写法的知识点总结: 一、创建和删除自定义单值类型 DB2中可以创建自定义单值类型,例如create distinct type var_newtype as decimal(5,2) with comparisons;该语句创建了一个...

    DB2常用的几条命令语句

    这篇博客主要讨论了DB2中的一些常用命令语句,对于理解和操作DB2数据库至关重要。在深入讲解这些命令之前,我们先来了解DB2的基础知识。 1. **安装与启动DB2** 在安装DB2后,可以通过`db2start`命令启动数据库服务...

    数据库DB2常用的命令

    DB2是一款由IBM开发的关系型数据库管理系统,广泛应用于...以上就是DB2常用的一些命令,熟悉并掌握这些命令,将极大地提升在DB2环境中的工作效率和数据管理能力。记得在操作数据库时要谨慎,确保数据的安全和完整性。

    db2 常用命令集合

    ### DB2常用命令详解 #### 一、启动与停止数据库 **启动数据库** - `db2start`:此命令用于启动DB2实例。 - `db2admin start`:这也是一个启动DB2实例的命令,但在某些场景下可能更为适用。 在实际使用中,通常...

Global site tag (gtag.js) - Google Analytics