-1

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

创建或替换过程 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
作为
开始

// 虚拟代码
打开 p_result FOR SELECT * FROM TBLSTATEMASTER;
如果不是 p_code IS NULL
OPEN p_result FOR SELECT * FROM p_result where code=p_code;
万一;

如果不是 p_name IS NULL
OPEN p_result FOR SELECT * FROM p_result where name=p_name;
万一;
结尾
/

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

4

2 回答 2

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;
于 2015-04-14T11:33:03.313 回答
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;
于 2015-04-14T14:38:07.330 回答