SQLPLUS 不打印受影响的行数

Posted

技术标签:

【中文标题】SQLPLUS 不打印受影响的行数【英文标题】:SQLPLUS Doesn't print number of rows affected 【发布时间】:2012-04-10 02:17:42 【问题描述】:

我在 .sql 文件中执行下面的脚本。我正在使用 Windows 命令行控制台来调用 sqlplus。当脚本终止时,一切看起来都很好,除了我看不到 INSERT 语句添加的记录数。您还可以在下面看到输出:

脚本

WHENEVER SQLERROR EXIT 1 ROLLBACK
WHENEVER OSERROR EXIT 1 ROLLBACK
SET FEEDBACK  ON 
SET VERIFY ON
BEGIN
DBMS_OUTPUT.put_line('Output Nothing');
END;
/
INSERT INTO .........

COMMIT;
QUIT;
/

输出显示

SQL*Plus: Release 10.2.0.1.0 - Production on Mon Apr 9 22:08:47 2012

Copyright (c) 1982, 2005, Oracle.  All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options

PL/SQL procedure successfully completed.

Commit complete.

Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64
bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options

当我在 TOAD/SQLNavigator 之类的工具上执行相同的 SQL 时,我可以看到添加的行数(参见下面的 ** 标记行)。

TOAD 上显示的输出

Processing ...
SET FEEDBACK  ON 

SQL*Plus command ignored.
Processing ...
SET VERIFY ON


SQL*Plus command ignored.
Processing ...
BEGIN
DBMS_OUTPUT.put_line('Doing Nothing');
END;

Doing Nothing
Processing ...
INSERT INTO .......

**11 row(s) inserted**

Processing ...
COMMIT

Processing ...
QUIT;

SQL*Plus command ignored.

您能否告诉我,即使我通过简单的'sqlplus' 运行此脚本,哪个设置可能会帮助我获取受此 SQL 影响的行数?

【问题讨论】:

INSERT inside plsql procedure does not tell how many rows were inserted的可能重复 但是,INSERT 似乎不在 pl/sql 块内。 【参考方案1】:

SQL*Plus 不会输出行数,但您可以通过以下方式显式执行此操作:

INSERT blah blah blah;
DBMS_OUTPUT.put_line (SQL%ROWCOUNT);

【讨论】:

【参考方案2】:

SET FEEDBACK ON 的默认阈值是 6。如果您想反馈更少的数字,请使用“SET FEEDBACK 1”。

【讨论】:

这对我没有任何影响。【参考方案3】:

使用

set echo on
set autotrace on 

在这种情况下显示执行的命令和有关语句的统计信息,即插入的行数

WHENEVER SQLERROR EXIT 1 ROLLBACK
WHENEVER OSERROR EXIT 1 ROLLBACK
SET FEEDBACK  ON 
SET VERIFY ON
set echo on
set autotrace on
BEGIN
DBMS_OUTPUT.put_line('Output Nothing');
END;
/
INSERT INTO .........

COMMIT;

QUIT;
/

【讨论】:

【参考方案4】:

似乎适用于我的 11g 客户端:

C:\>sqlplus user/pw

SQL*Plus: Release 11.2.0.1.0 Production on Mon Apr 9 21:12:12 2012

Copyright (c) 1982, 2010, Oracle.  All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> insert into testing (select 1,'XX' from dual connect by level < 11);

10 rows created.

SQL>

我认为 10g 客户端的工作方式没有什么不同,但我现在无法测试。

编辑:

与 10g SQLPlus 相同。我今天运行了你的确切脚本:

C:\>sqlplus user/pw@db

SQL*Plus: Release 10.2.0.1.0 - Production on Tue Apr 10 09:12:26 2012

Copyright (c) 1982, 2005, Oracle.  All rights reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

DB> WHENEVER SQLERROR EXIT 1 ROLLBACK
DB> WHENEVER OSERROR EXIT 1 ROLLBACK
DB> SET FEEDBACK  ON
DB> SET VERIFY ON
DB> BEGIN
  2      DBMS_OUTPUT.put_line('Output Nothing');
  3  END;
  4  /
Output Nothing

PL/SQL procedure successfully completed.

DB> insert into xx (select 'AA' from dual connect by level < 10);

9 rows created.

DB> COMMIT;

Commit complete.

DB> QUIT;
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

C:\>

我插入 1 行还是 9 行都没关系。我收到了消息。我猜您在脚本示例中遗漏了一些内容。 INSERT 是否在 BEGIN/END 块内?这会抑制消息。

【讨论】:

【参考方案5】:
rem to see Pl/SQL "print" statements:
set serveroutput on 

【讨论】:

以上是关于SQLPLUS 不打印受影响的行数的主要内容,如果未能解决你的问题,请参考以下文章

oracle数据库----光标

检查 CodeIgniter 中受影响的行数

返回受 UPDATE 语句影响的行数

结果不打印 Python - SQlplus

如何获取受 SQL Alchemy 影响的行数?

仅执行动态查询以获取受影响的行数