DB2执行SQL语句如何才不会报错呢?这是很多人都提到过的问题,下面就就教您DB2执行SQL语句并导出表记录的实现方法,供您参考。
很多人在console写DB2 SQL语句的时候习惯性的按enter换行,结果语句就直接执行报错.这是因为DB2默认的分隔符是空格,
如果要换行写语句,就必须在每行语句结尾加\,\表示换行.
还有一个写法就是连接DB2后,输入命令db2 -t, 这样就不需每行语句结尾都加\.只需按enter换行,语句写完后输入分号(;) 提交语句运行.
推荐使用下面的方式在DB2执行SQL语句并导出表记录.
- JXBI_js1:/db2home/mart796>touch crw.txt
- JXBI_js1:/db2home/mart796>db2 CONNECT TO jxmart USER ***** USING ******
- Database Connection Information
- Database server = DB2/AIX64 8.2.5
- SQL authorization ID = MART796
- Local database alias = JXMART
- ###db -t 命令指示语句允许换行,以分号;作结束符###
- JXBI_js1:/db2home/mart796>db2 -t
- (c) Copyright IBM Corporation 1993,2002
- Command Line Processor for DB2 SDK 8.2.5
- You can issue database manager commands and SQL statements from the command
- prompt. For example:
- db2 => connect to sample
- db2 => bind sample.bnd
- For general help, type: ?.
- For command help, type: ? command, where command can be
- the first few keywords of a database manager command. For example:
- ? CATALOG DATABASE for help on the CATALOG DATABASE command
- ? CATALOG for help on all of the CATALOG commands.
- To exit db2 interactive mode, type QUIT at the command prompt. Outside
- interactive mode, all commands must be prefixed with 'db2'.
- To list the current command option settings, type LIST COMMAND OPTIONS.
- For more detailed help, refer to the Online Reference Manual.
- db2 => EXPORT TO "/db2home/mart796/crw.txt" OF DEL MODIFIED BY coldel0x09
- db2 (cont.) => SELECT RN_USER_ID,
- db2 (cont.) => HIS_USER_ID,
- db2 (cont.) => RN_USER_NUMBER,
- db2 (cont.) => HIS_USER_NUMBER,
- db2 (cont.) => RN_BASE_COUNTY_CODE,
- db2 (cont.) => RN_BASE_PLAN_ID,
- db2 (cont.) => RN_CREATE_DATE
- db2 (cont.) => FROM JA.MARTD_DMRN_USER_MS
- db2 (cont.) => WHERE RN_DATE = '2008-07-01'
- db2 (cont.) => ;
- SQL3104N The Export utility is beginning to export data to file
- "/db2home/mart796/crw.txt".
- SQL3105N The Export utility has finished exporting "554848" rows.
- Number of rows exported: 554848
- db2 => quit;
- DB20000I The QUIT command completed successfully.
到此,总共导出584848条记录.接下来的便是把记录get 下来,导入到本地的ORACLE数据库.比较简单.
【编辑推荐】