在PLSQL中使用编译误差创建的函数



当我编译以下代码时,我将收到错误消息" 用编译错误创建的函数"

create or replace function find_port(ip_ID in int) RETURN int
is
    t_count number;
    count varchar;
begin
    select is_rail into count from table where id = ip_ID;
    case 
        when count ='True' then t_count:=1;
        when count ='False' then t_count:=0;
    end case;
end;
/

我正在收到一条错误消息"创建了编译错误的函数"

所以您应该问的问题是,"我如何获得我的PL/SQL代码的汇编错误列表?"

其他人告诉您如何解决代码中的当前错误,但更重要的技能是您了解如何自己诊断自己的代码。

oracle是一个数据库,它将元数据存储在一组称为数据字典的特殊视图中。这些视图包括汇编错误的视图。此查询将在任何SQL环境中工作:

select name, type, line, text -- or just *, obvs
from user_errors ue
order by ue.name, ue.type, ue.sequence;

也有all_errors和dba_errors的视图。了解更多。

在SQL*中,您可以运行sho errshow errors的缩写)。诸如PL/SQL开发人员或Oracle SQL开发人员之类的IDE将自动显示汇编错误。

一旦知道如何获取错误的文本,您需要知道LINE将告诉您错误的位置。尽管由于某些类别的错误(例如缺少逗号或无与伦比的括号),指示的行可能不是实际错误所在的行。不幸的是,仍然需要解释和理解,这需要经验。

实际上,计数可以用作PL/SQL变量:

SQL> create or replace function f_test return int is
  2    count number;
  3  begin
  4    select 1 into count from dual;
  5    return 2;
  6  end;
  7  /
Function created.
SQL> select f_test from dual;
    F_TEST
----------
         2
SQL>

但是,您不能返回 it:

SQL> create or replace function f_test return int is
  2    count number;
  3  begin
  4    select 1 into count from dual;
  5    return count;
  6  end;
  7  /
Warning: Function created with compilation errors.
SQL> show err
Errors for FUNCTION F_TEST:
LINE/COL ERROR
-------- -----------------------------------------------------------------
5/3      PL/SQL: Statement ignored
5/10     PLS-00204: function or pseudo-column 'COUNT' may be used inside a
         SQL statement only
SQL>

在这里,@priya,您可以看到如何帮助自己 - 显示ERR会告诉您您的代码有什么问题。

除此之外,您使用的案例语句是无效编写的;应该与此相似:

SQL> create or replace function f_test return int is
  2    l_count number;
  3    t_count number;
  4  begin
  5    select 1 into l_count from dual;
  6
  7    t_count := case when l_count = 1 then 1
  8                    when l_count = 2 then 2
  9               end;
 10
 11    return t_count;
 12  end;
 13  /
Function created.
SQL> select f_test from dual;
    F_TEST
----------
         1
SQL>

count是一个SQL函数,因此不是将其用作PL/SQL变量的更好选择。CASE块可以在Select语句中使用。

此外,您的函数不会RETURN任何值。

 create or replace function find_port(ip_ID in int) RETURN int
  is
      t_count number;
  begin
      select case 
         when is_rail = 'True' then  1
         when is_rail = 'False' then 0
         end  into t_count from yourtable where id=ip_ID;
      RETURN t_count;
 end;

最新更新