问题
Please suppose you have an Oracle PL/SQL package of about 200,000 rows of code.
Is there any fast way to detect variables declared, but not used in the package?
Thank you in advance for your kind help.
EDIT (April 7th, 2014): I am using Oracle 10G.
EDIT: I am looking for a pure PL/SQL solution.
回答1:
The following only applies to 11g R2. It looks like PL/Scope has become available in 11g R1.
You won't get information about unused variables with 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>
As you can see the only reported warning is not related to the unused variables at all. Instead PL/Scope has to be used.
The following example has bee derived from 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>
The script plsql-unused-variables.sql
is just a cut and paste from the blog post mentioned above. Because I found it useful I have also made the script available in Bitbucket.
回答2:
Set your session to report all warnings:
ALTER SESSION SET PLSQL_WARNINGS='ENABLE:ALL';
then compile your code. If the compilation indicates that there are errors, as in you get something like the following:
SP2-0804: Procedure created with compilation warnings
display any errors:
SHO ERR
If you have any unreferenced variables they should be mentioned in the list of errors. Alternatively, use a tool like PL/SQL Developer which automatically shows you these errors following a compile.
Share and enjoy.
回答3:
PL/SQL waits to allocate memory until you assign the variable, then only allocates as much storage as needed. So no need to worry about consuming memory. (Source)
So finding and fixing unused variables is just a housekeeping exercise and won't improve the performance of the package. In other words, the simplest solution would be to do nothing.
If you're worried about unused variables anyway, you might be able to find them just by parsing the package source with command-line tools such as grep
, sort
and uniq
(especially if they follow a coding standard such as starting all variables with v_
).
来源:https://stackoverflow.com/questions/22868567/oracle-pl-sql-how-to-find-unused-variables-in-a-long-package