在Oracle sql中使用declare和if语句?

时间:2015-04-08 15:49:43

标签: oracle stored-procedures

您好我正在运行此查询。我试图获取输入参数,并基于该值在查询中使用它。

我得到的错误是:

**Error(26,3): PLS-00103: Encountered the symbol "IF" when expecting one of the following: begin function pragma procedure subtype type <an identifier> <a double-quoted delimited-identifier> current cursor delete exists prior **

create or replace 
PROCEDURE GET_STATES (
    i_id IN NUMBER,
    o_result OUT SYS_REFCURSOR,

IS

DECLARE country VARCHAR2 = '';

BEGIN
  o_sqlmsg  := 'SUCCESS';
  o_sqlcode := 0;

  IF i_id == 284 
    THEN country := 'US';
    ELSE country := 'CA';
  END IF;
  OPEN o_result FOR 
    Select cnint as Id, cnabbv as Code, rtrim(cnname) as Name, i_id as CId
    from country 
    order by cnname;

1 个答案:

答案 0 :(得分:1)

您的程序中不应包含DECLARE关键字,并且缺少最终的END语句以及其他一些小的语法问题。将其更改为类似

create or replace 
PROCEDURE GET_STATES (i_id IN NUMBER,
                      o_result OUT SYS_REFCURSOR)
IS
  country VARCHAR2;
BEGIN
  o_sqlmsg  := 'SUCCESS';
  o_sqlcode := 0;

  IF i_id = 284 
    THEN country := 'US';
    ELSE country := 'CA';
  END IF;

  OPEN o_result FOR 
    Select cnint as Id,
           cnabbv as Code,
           rtrim(cnname) as Name,
           i_id as CId
    from country 
    order by cnname;
END GET_STATES;

祝你好运。