oracle - PL/SQL - 当存在歧义时我可以收到警告吗

标签 oracle plsql oracle-sqldeveloper toad

我有以下代码:

create table test_table (
  col_name   varchar2(20) not null
);
insert into test_table values ('Value 1');
insert into test_table values ('Value 2');
insert into test_table values ('Value 3');
insert into test_table values ('Value 4');
commit;
/
declare
  col_name   varchar2(20) := 'Value 1';
begin
  for c in (select col_name from test_table where test_table.col_name = col_name) loop
    dbms_output.put_line('Row ' || c.col_name);
  end loop;
  commit;
end;
/

在此代码中,where 子句始终为 true。显然这不是本意。是否可以让编译器警告我这一点?

最佳答案

让我们运行您的示例(在 Oracle 11gR2 中)。

测试表

create table test_table (
  col_name   varchar2(20) not null
);

insert into test_table values ('Value 1');
insert into test_table values ('Value 2');
insert into test_table values ('Value 3');
insert into test_table values ('Value 4');

打开所有编译器警告

SQL> alter session set plsql_warnings = 'ENABLE:ALL';

测试程序

SQL> ed
Wrote file /tmp/afiedt.buf

  1  create or replace procedure test_proc is
  2    col_name   varchar2(20) := 'Value 1';
  3  begin
  4    for c in (select col_name from test_table where test_table.col_name = col_name) loop
  5      dbms_output.put_line('Row ' || c.col_name);
  6    end loop;
  7    commit;
  8* end;
SQL> /

SP2-0804: Procedure created with compilation warnings

Elapsed: 00:00:00.17
SQL> show errors
Errors for PROCEDURE TEST_PROC:

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

SQL>

结论

抱歉,编译器不会帮助您:((我们只收到不相关的警告。)

显然您知道 PL/SQL 名称解析是个好东西 documented feature :)

为了防止这种情况发生,您在 PL/SQL 中可以做的最好的事情就是完全限定 SQL 语句中的所有列和 PL/SQL 变量。在代码审查中强制执行该政策。

关于oracle - PL/SQL - 当存在歧义时我可以收到警告吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39019043/

相关文章:

oracle - 突出显示两个字符串之间的差异

oracle - 为 Oracle 存储过程依赖关系图生成 DDL

c# - 轮询 Oracle 数据库以获取更改

oracle - 在 PL/SQL 中动态创建 View 并使用 CLOB 时出现 ORA-00907

oracle - 无法在 Debug 中修改该值

oracle - SQL 开发人员 : cant open the program

sql - 将两个选择查询合二为一

sql - 通过数据库链接描述?

sql - Oracle SQL/PLSQL : Hierarchical recursive query with repeating data

java - 如何在没有jpa的情况下在spring boot上从oracle获取数据