【问题标题】:Oracle cursor error with if statement带有 if 语句的 Oracle 游标错误
【发布时间】:2018-06-04 08:34:44
【问题描述】:

尝试在包中创建此过程时出现错误。

FUNCTION SEARACH_FOR_GAMES (p_search_string in varchar2, p_match_type in number )
                             return weak_cur
  IS
    SEARCH_FIXID WEAK_CUR;   
  BEGIN  


    if p_match_type = 2
    then 
    OPEN   SEARCH_FIXID FOR
        select  FIXID, HOME,AWAY,COMP_NAME, M_TIME from SOCCER s
        where s.HOME LIKE (p_search_string) or s.AWAY LIKE (p_search_string)
        union all
        select  FIXID,HOME,AWAY,LISTS,M_TIME from BASKETBALLb
        where b.HOME LIKE (p_search_string) or b.AWAY LIKE (p_search_string)
        union all
        select FIXID,HOME,AWAY,COMP,M_TIME from HANDBALL h
        where h.HOME LIKE (p_search_string) or h.AWAY LIKE (p_search_string);
   elsif p_match_type = 1
    then
    OPEN   SEARCH_FIXID FOR
        select  FIXID,HOME,AWAY,COMP_NAME, TIME from LIVE_MATCHES_TZ s
        where s.HOME LIKE (p_search_string) or s.AWAY LIKE (p_search_string)
        union all
        select  FIXID,HOME,AWAY,COMP_NAME,TIME from LIVE_BASKETBALL_MATCHES b
        where b.HOME LIKE (p_search_string) or b.AWAY LIKE (p_search_string)
        union all
        select FIXID,HOME,AWAY,COMP_NAME,TIME from LIVE_HANDBALL_MATCHES h
        where h.HOME LIKE (p_search_string) or h.AWAY LIKE (p_search_string);
    end if;

    RETURN SEARCH_FIXID;
  END SEARACH_FOR_GAMES;

我得到两个错误在预期以下之一时遇到符号“IF”并遇到符号“文件结尾”。 难道是因为它的光标?

【问题讨论】:

标签: oracle plsql cursor


【解决方案1】:

一定是这样的:

if p_match_type = 0 then
    OPEN SEARCH_FIXID FOR
    select  FIXID,HOME,AWAY,COMP_NAME, M_TIME 
    from SOCCERs
    where s.HOME LIKE (p_search_string) or s.AWAY LIKE (p_search_string)
        ...

为了使用光标,你必须检查它

cur := PCK_BET.SEARACH_FOR_GAMES('xyz', 1);
IF cur%IS_OPEN THEN
    ...
ELSE
    -- No cursor opened
END ID;

【讨论】:

  • 但是如果我在这个下有另一个 IF .. 如果 p_match_type = 1 然后从 TABLE1 中选择 FIXID,HOME,AWAY,COMP_NAME, M_TIME s.HOME LIKE (p_search_string) 或 s.AWAY LIKE (p_search_string) union all select FIXID,HOME,AWAY,LISTS,M_TIME from TABLE2 b where b.HOME LIKE (p_search_string) or b.AWAY LIKE (p_search_string) end if;我不需要再次打开光标?
  • 不,当然不是。我只是懒得复制你所有的大查询。
  • 但是当不将打开的光标放在 THEN 中时,我得到错误,此选择中需要 INTO 子句
【解决方案2】:

这是

open cursorname for select...;

不是

open cursorname;

for
    [...procedural logic...]

由于我们可以依靠优化器将这样的谓词推送到视图中,所以我会将其写成这样:

create or replace function search_for_games
    ( p_search_string   in varchar2
    , p_match_type      in number )
    return sys_refcursor
is
    search_results sys_refcursor;
begin
    open search_results for
        select fixid, home, away, comp_name, m_time
        from   (
                 -- Type 0 matches:
                 select 0 as matchtype, fixid, home, away, comp_name, m_time from soccers
                 union all
                 select 0 as matchtype, fixid, home, away, comp_name, m_time from basketballb
                 union all
                 select 0 as matchtype, fixid, home, away, comp_name, m_time from handball h
                 union all
                 select 0 as matchtype, fixid, home, away, comp_name, m_time from ice_hockey i
                 union all
                 select 0 as matchtype, fixid, home, away, comp_name, m_time from tenis t
                 union all
                 select 0 as matchtype, fixid, home, away, comp_name, m_time from volleyball v
                 union all
                 -- Type 1 matches:
                 select 1 as matchtype, fixid, home, away, comp_name, m_time from table1
                 union all
                 select 1 as matchtype, fixid, home, away, comp_name, m_time from table2
               ) m
        where  m.matchtype = p_match_type
        and    ( m.home like p_search_string or m.away like p_search_string );

    return search_results;
end search_for_games;

where 子句将根据需要复制到各个部分。

我不确定您需要使用p_match_type 参数实现什么逻辑,但无论如何,上面的结构应该是一个开始。

【讨论】:

  • 我编辑了我的代码,你能检查一下吗?当我评论第二个 OPEN SEARCH_FIXID FOR where p_match_type = 1 时,我收到错误,我需要插入语句。但是在 elsif 语句中使用这些打开的游标可以正常工作..
  • 我认为IF 条件比您的查询要快。
  • 我真的不明白为什么会这样。但无论如何,OP 希望避免重复光标分配,并且这种结构强制一致性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-17
  • 1970-01-01
  • 2021-11-26
相关资源
最近更新 更多