编译SQL函数 - 错误:PLS-00103:遇到符号&#34 ;;"期待下列之一:案例

时间:2014-07-30 14:45:30

标签: oracle plsql

我试图编译以下函数:

CREATE OR REPLACE FUNCTION where_to_save(v_part_dynam OUT CLOB)
RETURN CLOB
IS
BEGIN 
  CASE
      WHEN LDR_SUBTYPE = ('IEC') THEN v_part_dynam := v_part_dynam || 'SUBPARTITION SP0 VALUES (IEC)';

      ELSE  v_part_dynam := v_part_dynam || 'SUBPARTITION SP1 VALUES (OTHER)';
  END
   RETURN v_part_dynam;   

END where_to_save;

脚本输出打印出已编译的函数,但出现错误

FUNCTION WHERE_TO_SAVE compiled
Errors: check compiler log

错误是PLS-00103

Error(10,4): PLS-00103: Encountered the symbol ";" when expecting one of the following:     case 

我不知道为什么会发生这种情况,我不能只是因为......问题是什么?

2 个答案:

答案 0 :(得分:2)

您的代码的具体问题是,当用作PL / SQL语句时,CASE必须以END CASE;终止。

BEGIN 
  CASE
      WHEN LDR_SUBTYPE = ('IEC') THEN v_part_dynam := v_part_dynam || 'SUBPARTITION SP0 VALUES (IEC)';

      ELSE  v_part_dynam := v_part_dynam || 'SUBPARTITION SP1 VALUES (OTHER)';
  END CASE;

  RETURN v_part_dynam;   

END where_to_save;

答案 1 :(得分:1)

作业应该在case之前,在PL / SQL中你可以使用if

CREATE OR REPLACE FUNCTION where_to_save(v_part_dynam OUT CLOB)
RETURN CLOB
IS
BEGIN 
  if LDR_SUBTYPE = ('IEC')
  then
    v_part_dynam := v_part_dynam || 'SUBPARTITION SP0 VALUES (IEC)';
  else
    v_part_dynam := v_part_dynam || 'SUBPARTITION SP1 VALUES (OTHER)';
  end if;

  RETURN v_part_dynam;   
END where_to_save;

或者如果您更喜欢case

CREATE OR REPLACE FUNCTION where_to_save(v_part_dynam OUT CLOB)
RETURN CLOB
IS
BEGIN 
  case
  when LDR_SUBTYPE = ('IEC')
  then v_part_dynam := v_part_dynam
       || 'SUBPARTITION SP0 VALUES (IEC)';
  else v_part_dynam := v_part_dynam
       || 'SUBPARTITION SP1 VALUES (OTHER)';
  end case;
  RETURN v_part_dynam;   
END where_to_save;

此外,您的代码中未定义LDR_SUBTYPE。这应该。这个签名可行:

CREATE OR REPLACE FUNCTION where_to_save(LDR_SUBTYPE varchar, v_part_dynam OUT CLOB)