Oracle select语句基于多个输入参数

时间:2015-04-14 11:21:45

标签: oracle plsql oracle11g ref-cursor

我如何在oracle 11g中实现以下功能。

CREATE OR REPLACE PROCEDURE sel_tblStateMaster (
p_id                IN       NUMBER DEFAULT NULL, 
p_code                IN       varchar DEFAULT NULL,
p_name                IN       varchar DEFAULT NULL),
p_result     OUT      sys_refcursor
as
begin

// Dummy code
OPEN p_result FOR SELECT * FROM TBLSTATEMASTER;
if not p_code IS NULL
OPEN p_result FOR SELECT * FROM p_result where code=p_code;
end if;

if not p_name IS NULL
OPEN p_result FOR SELECT * FROM p_result where name=p_name;
end if;
end
/

我知道上面指定的查询不正确,我只需要一个针对Oracle的更正解决方案。

2 个答案:

答案 0 :(得分:0)

这是一个简单的 IF-ELSE 结构:

IF p_code IS NOT NULL AND p_name IS NOT NULL 
THEN
  OPEN p_result FOR SELECT * FROM TBLSTATEMASTER WHERE code=p_code AND NAME=p_name;
ELSE
  OPEN p_result FOR SELECT * FROM TBLSTATEMASTER;
END IF;

答案 1 :(得分:0)

IF语句应该有一个then子句

    CREATE OR REPLACE PROCEDURE sel_tblStateMaster (
    p_id                IN       NUMBER DEFAULT NULL, 
    p_code                IN       varchar DEFAULT NULL,
    p_name                IN       varchar DEFAULT NULL),
    p_result     OUT      sys_refcursor
    as
    begin

    // Dummy code
    OPEN p_result FOR SELECT * FROM TBLSTATEMASTER;
    if not p_code IS NULL
    then
    OPEN p_result FOR SELECT * FROM p_result where code=p_code;
    end if;

    if not p_name IS NULL
    then
    OPEN p_result FOR SELECT * FROM p_result where name=p_name;
    end if;
    end;