gpt4 book ai didi

variables - Oracle PL/SQL : How to find unused variables in a long package?

转载 作者:行者123 更新时间:2023-12-02 23:00:30 24 4
gpt4 key购买 nike

请假设您有一个包含大约 200,000 行代码的 Oracle PL/SQL 包。

有没有快速的方法来检测已声明但未在包中使用的变量?

预先感谢您的帮助。

编辑(2014 年 4 月 7 日):我正在使用 Oracle 10G。

编辑:我正在寻找纯粹的 PL/SQL 解决方案。

最佳答案

以下仅适用于11g R2。看起来像 PL/Scope has become available in 11g R1 .

您不会通过 PLSQL_WARNINGS='ENABLE:ALL' 获得有关未使用变量的信息:

SQL> !cat test.sql
set serveroutput on

alter session set plsql_warnings = 'ENABLE:ALL';

create or replace procedure foo is
v_a number;
v_b varchar2(10);
begin
dbms_output.put_line('hello world!');
end;
/
show errors

exec foo

SQL> @test

Session altered.


SP2-0804: Procedure created with compilation warnings

Errors for PROCEDURE FOO:

LINE/COL ERROR
-------- -----------------------------------------------------------------
1/1 PLW-05018: unit FOO omitted optional AUTHID clause; default
value DEFINER used

hello world!

PL/SQL procedure successfully completed.

SQL>

正如您所看到的,唯一报告的警告与未使用的变量根本无关。相反PL/Scope必须使用。

以下示例源自 Oracle 11g – Generating PL/SQL Compiler Warnings (Java style) using PL/Scope :

SQL> alter session set plscope_settings = 'identifiers:all';

Session altered.

SQL> alter procedure foo compile;

SP2-0805: Procedure altered with compilation warnings

SQL> show errors
Errors for PROCEDURE FOO:

LINE/COL ERROR
-------- -----------------------------------------------------------------
1/1 PLW-05018: unit FOO omitted optional AUTHID clause; default
value DEFINER used

SQL> @plsql-unused-variables.sql
Enter value for name: foo
old 10: where object_name = upper('&name')
new 10: where object_name = upper('foo')
Enter value for type: procedure
old 11: and object_type = upper('&type')
new 11: and object_type = upper('procedure')

COMPILER_WARNING
--------------------------------------------------------------------------------
V_B: variable is declared but never used (line 3)
V_A: variable is declared but never used (line 2)

SQL>

脚本plsql-unused-variables.sql只是从上面提到的博客文章中剪切和粘贴的。因为我发现它很有用,所以我还在 Bitbucket 中提供了该脚本。 .

关于variables - Oracle PL/SQL : How to find unused variables in a long package?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22868567/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com