当前位置:文档之家› DB2常用命令

DB2常用命令

进入编辑器:
edit
基本语句:
db2 >>update pc1121 set BAD_DEBT_TERM=1,BAD_DEBT_AMT=DUE_AMT-REAL_REPAYMENT,STAT='2',STAT_24M='1' where DUE_AMT>REAL_REPAYMENT and Trim(STAT_24M)='N' (db2的缓存功能)
连接数据库:connect to db_name(数据库) user hxusr(用户) using hxusr1(口令)

A中的表数据想要根据B表的数据相应进行update:
update A set bal=(select txnamt from B where actno=A.actno and cnlno=https://www.doczj.com/doc/652423987.html,lno) where A.actno||https://www.doczj.com/doc/652423987.html,lno in (select Actno||cnlno from B )

db2版本:
db2level

创建表结构:
db2 create table table_name
(
sno int identity(1,1) primary key, --自动增加字段,
sname varchar(30) not null ,
sdate date not null
--CONSTRAINT P_KEY_1 PRIMARY KEY (sno)
);

插入记录:
db2 insert into table_name(sno,sname) values(1,'Sequen'),(2,'Jacson')

设置当前模式名:
db2 connect to MyDB2
db2 set current schema UT001

查询模式名:
db2 select distinct tabschema from syscat.tables

表结构的改变:
alter table 表名 add 字段 类型
alter table 表名 drop column 字段
alter table 表名 alter column 字段 set data type 类型【char(10)】;只能把长度往上加
alter table 表名 add primary key(字段)
alter table 表名 drop primary key(字段)

db2 "reorg table b_mjour_c" 对表结构进行修改后要对表进行表重构

给表重命名:
rename table 表名 to 新表名
----------------------------------------
字符串--》时间
date(to_date(proc_dat,'yyyy-mm-dd')) + 1 days/months/years
to_date(column,'yyyy-mm-dd') :TIMESTAMP_FORMAT

***************************************
DB2设置客户端工具命令行:编目
db2 catalog tcpip node node70 remote 96.0.40.70 server 50000
db2 terminate
db2 catalog db reportdb as rpt70 at node node70
db2 terminate
注意:在Cognos编目数据库时数据库名不能重命名!
服务器的IP地址可以在C:\WINDOWS\system32\drivers\etc\hosts用变量代替
***************************************
解Load锁:
db2>> load from /dev/null of del (terminate/insert/replace/restart) into TABLE_NAME
load from /dev/null of del terminate into TABLE_NAME
load from /dev/null of del restart into TABLE_NAME
load from /dev/null of del replace into TABLE_NAME
load from /dev/null of del insert into TABLE_NAME
db2>> get snapshot for locks on reportdb
db2 list application show detail | grep irge01
db2>> force application(20570)

查看表是否锁:load query table table_name

清光表数据:import from /dev/null of del replace into table_name

表空间清单:db2 list tablespaces show detail

表的目录:$ db2 "list node directory"

数据库清单:$ db2 list db directory

执行*.sql 文件:db2 -tvf *.sql

备份数据库reportdb:$db2 backup db reportdb
恢复数据库reportdb:$db2 restore db reportdb

导出表结构:$db2look -d reportdb -e -l -u hxusr -t Par_table -o Par_table.sql

(db2look -d reportdb -e -z hxusr -t Par_table -o Par_table.sql)

导出表数据:db2move reportdb export -tn table1 table2
导入表数据:db2move reportdb import

导出表数据:$ db2 "export to data_eftm of del select * from b_seftm" [导出表数据的路径,文件](一般导出来的是ixf格式的数据文件)
导入表数据:$ db2 "load from data_eftm of del insert into hxusr.b_seftm" [导入表数据的路径,文件]

建立索引:
create index "Index_INVM_ACCT_NO" on B_SSINVM (INVM_ACCT_NO ASC);
Create Unique Index IRGE03_IDX0 On Irge03( datetran,subsys,brcmgm,ccy,subnoc,stsacc,accsub );
db2 runstats on table 模式名.表名 and indexes all 请在重建表后,导入当天数据,并执行前面的脚本更新该表的统计信息。

查找hxusr模式名下的所有表:db2 "list tables for schema hxusr ";

select中创建序号:select ROW_NUMBER()OVER(【分组】partition by brhid 【排序】order by acct_no) as a,acct_no,br_name from b_sbrhm
select中判断字段输出值:select (case when mat_dt=999999999 then 2958464 else mat_dt end) as mat_dt from b_mtermacctinfo
测试decimal处理定长:
db2 "values char(decimal('6.000',9,4))" :00006.0000
db2 "values char(cast(7 as decimal(7,3))) " : 0007.000

EXPORT.out IMPORT.out LOAD.out db2move.lst tab1.ixf tab1.msg
$ db2move reportdb import

in/not in,exists/not exists:
IN:确定给定的值是否与子查询或列表中的值相匹配;
使用 EXISTS 和 NOT EXISTS 引入的子查询可用于两种集合原理的操作:交集与差集。两个集合的交集包含同时属于两个原集合的所有元素.
SELECT ProductID, ProductName FROM Northwind.dbo.Products WHERE CategoryID IN (1, 4, 5)
NOT EXISTS:
例如,要查找不出版商业书籍的出版商的名称:
SELECT pub_name FROM publishers WHERE NOT EXISTS (SELECT * FROM titles WHERE pub_id = publishers.pub_id AND type = 'business')

union 和union all的区别是 union会合并重复行;

db2 "values round(76146.935600,2)"-->76146.940000

db2 create table fnsonlqd.vpar2 like fnsonlqd.vpar in TBS_TSMISC; 建立一个表结构fnsonlqd.vpar2与表结构fnsonlqd.vpar一样的。

db2 "export to B_SCUSM.txt of del modified by nochardel select * from B_SCUSM" 导出B_SCUSM.txt数据文本且字段数据是定长度无引号的.

replace(custnam,'|','')将custnam中的'|'用''替换

Trim(char(bigint(bal)))||'.'||right(Trim(char(bigint(bal*100))),2) 将bal(金额类型decimal)转化为字符类型

db2 ? sql0803 查询报错语句

db2 "GRANT SELECT,INSERT,UPDATE,DELETE,ALTER,INDEX,REFERENCES ON TABLE PBCDWDB.CMREPORTGROUP(表) TO USER PBCDWDB(用户)" 赋予用户权限

在bnd目录下需传bnd文件 bind dbapi.bnd 绑定

db2 在创建表中一般不用"table_name"来创建表,如果用了会对大小写敏感

db2 get db cfg for 'database' 获取该数据库database中配置参数
db2 update db cfg for sample(数据库) using LOGF

ILSIZ(参数) 50000 修改该数据库的配置参数


db2 set integrity for XX immediate checked 查询报表时报reason code 1错误时用
db2 "load from /dev/null of del terminate into s_structure" 查询报表时报reason code 3错误时用

db2 "import from filename of del [modified by nochardel(chardel)"][modified by coldel|][modified by norowwarnings屏蔽行警告] warningcount 1 insert into tablename"在加载数据的时候假如有警告或者错误(类型不匹配,列不对应造成的)条数超过warningcount就会停止

grep db2c_db2inst1 /etc/services 查找db2端口号

db2 "select current date from sysibm.sysdummy1" 取出当前数据库日期

db2 "values ''''"转义单引号两个单引号输出一个单引号

查看db2端口:
1.db2 get dbm cfg | grep -i service 获取TCP/IP Service name 【pbcdw】
2.grep pbcdw /etc/services

存储过程编译执行:
db2 -td@ -vf whiles.db2 编译
db2 "CALL dept_median (51, ?)" 执行

db2set db2codepage=1386
设置数据库的概要注册信息,该信息要与数据库的配置信息一致,否则在用“load”工具加载数据文件时,系统提示“codepage”错误

SQL1652N发生文件I/O错误 文件没有授予执行权限





相关主题
文本预览
相关文档 最新文档