DB2 CLI结果输出

klr1opcd  于 2022-11-07  发布在  DB2
关注(0)|答案(3)|浏览(179)

当在MySQL中运行命令行查询时,可以选择使用'\G'作为语句终止符,并且结果集列不是水平地在屏幕上列出,而是垂直地列出每一列,相应的数据在右边。有没有方法可以使用DB2命令行实用程序来实现相同或类似的操作?
常规MySQL结果示例

  1. mysql> select * from tagmap limit 2;
  2. +----+---------+--------+
  3. | id | blog_id | tag_id |
  4. +----+---------+--------+
  5. | 16 | 8 | 1 |
  6. | 17 | 8 | 4 |
  7. +----+---------+--------+

替代MySQL结果示例:

  1. mysql> select * from tagmap limit 2\G
  2. ***************************1. row***************************
  3. id: 16
  4. blog_id: 8
  5. tag_id: 1
  6. ***************************2. row***************************
  7. id: 17
  8. blog_id: 8
  9. tag_id: 4
  10. 2 rows in set (0.00 sec)

显然,当列是大字符串或结果集中有许多列时,这会更有用,但这比我可能解释的更好地演示了格式。

bsxbgnwa

bsxbgnwa1#

我认为DB2命令行客户机没有提供这样的选项。有关一些建议,请参阅http://www.dbforums.com/showthread.php?t=708079。有关DB2命令行客户机的更通用的信息,可以查看IBM DeveloperWorks文章DB2's Command Line Processor and Scripting

idfiyjo8

idfiyjo82#

有点晚了,但发现这篇文章时,我搜索了一个选项,以检索只选定的数据。
所以db2 -x <query>只返回结果。更多的选项可以在这里找到:https://www.ibm.com/docs/en/db2/11.1?topic=clp-options
示例:

  1. [db2inst1@a21c-db2 db2]$ db2 -n select postschemaver from files.product
  2. POSTSCHEMAVER
  3. --------------------------------
  4. 147.3
  5. 1 record(s) selected.
  6. [db2inst1@a21c-db2 db2]$ db2 -x select postschemaver from files.product
  7. 147.3
euoag5mw

euoag5mw3#

DB2命令行实用程序总是以表格格式显示数据,即水平显示行,垂直显示列。它不支持任何其他格式,如\G语句终止符为mysql所做的。但是,当设置DB2_WORKLOAD=ANALYTICS时,可以在DB2表中存储按列组织的数据。

  1. db2 => connect to coldb
  2. Database Connection Information
  3. Database server = DB2/LINUXX8664 10.5.5
  4. SQL authorization ID = BIMALJHA
  5. Local database alias = COLDB
  6. db2 => create table testtable (c1 int, c2 varchar(10)) organize by column
  7. DB20000I The SQL command completed successfully.
  8. db2 => insert into testtable values (2, 'bimal'),(3, 'kumar')
  9. DB20000I The SQL command completed successfully.
  10. db2 => select * from testtable
  11. C1 C2
  12. ----------- ----------
  13. 2 bimal
  14. 3 kumar
  15. 2 record(s) selected.
  16. db2 => terminate
  17. DB20000I The TERMINATE command completed successfully.
展开查看全部

相关问题