本文主要总结当有批量 SQL 需要通过客户端执行时(例如通过 source),如何清楚列出 SQL 文件中每一条 SQL 对应哪条执行结果,特别是执行的 SQL 数量较多或执行出错时。
OceanBase 并没有提供 sqlplus spool 或 db2 -v 命令回显功能,但我们可以通过 tee 结合 \p 来实现。
tee 指定日志文件来记录 SQL 执行的结果
MySQL [test]> tee test.txt; Logging to file 'test.txt' MySQL [test]> notee Outfile disabled.在 SQL 语句后添加
\p来打印 SQL 语句本身MySQL [test]> select current_timestamp\p; -------------- select current_timestamp -------------- +---------------------+ | current_timestamp | +---------------------+ | 2023-07-20 15:31:12 | +---------------------+ 1 row in set (0.01 sec) MySQL [test]> select current_timestamp; +---------------------+ | current_timestamp | +---------------------+ | 2023-07-20 15:31:17 | +---------------------+ 1 row in set (0.00 sec)具体示例如下:
[root@observer ~]# cat 1.sql select version()\p; [root@observer ~]# mysql -h127.1 -uroot@mysql -P2881 -pxxx -c test MySQL [test]> tee test.txt; Logging to file 'test.txt' MySQL [test]> source test.sql; -------------- select version() -------------- +---------------------------+ | version() | +---------------------------+ | 5.7.25-OceanBase-v3.2.4.4 | +---------------------------+ 1 row in set (0.00 sec) MySQL [test]> notee Outfile disabled. MySQL [test]> exit [root@observer ~]# cat test.txt MySQL [test]> source test.sql; -------------- select version() -------------- +---------------------------+ | version() | +---------------------------+ | 5.7.25-OceanBase-v3.2.4.4 | +---------------------------+ 1 row in set (0.00 sec)
MySQL 命令选项:

适用版本
OceanBase 数据库的所有版本。