添加IF ELSE声明

时间:2020-09-04 06:44:06

标签: sql oracle plsql

我在PHP中有类似的功能,

if (  $busyUsersIDS ) {
 SELECT statment 
ELSE {
SELECT statment
 }

到目前为止,我将其转换为可在Oracle SQL中使用的功能,现在的主要问题是添加IF ELSE,这对我来说像初学者一样非常困难,无论我尝试什么,总是会出错

FUNCTION GET_MOMENT_PROJECT_USERS(p_busyUsersID IN INT)
   RETURN SYS_REFCURSOR IS 
    rc  SYS_REFCURSOR;
    /*getBusyUsersIDS*/
 BEGIN
  OPEN rc FOR
    CASE bussyUserID WHEN bussyUserID THEN
        SELECT u.*,
                ur.Roles 
        FROM 
                users u 
        INNER JOIN 
                user_roles ur 
        ON 
                u.User_roleID = ur.id 
        WHERE 
                u.UserID NOT IN (p_busyUsersID) 
        AND 
                u.Surname IS NOT NULL
        AND 
                u.Lastname IS NOT NULL 
        ORDER BY
                u.ProjectPlanSortIndex;

        ELSE  
                            
        SELECT  u.*, 
                ur.Roles 
        FROM 
                users u 
        INNER JOIN 
                user_roles ur 
        ON 
                u.User_roleID = ur.id 
        WHERE 
                u.Surname IS NOT NULL 
        AND 
                u.Lastname IS NOT NULL 
        ORDER BY 
                u.ProjectPlanSortIndex;
     END;
  RETURN rc;
END GET_BUSY_USERS_IDS;

到目前为止,我遇到了两个错误

错误(524,9):PLS-00103:预期以下情况之一时遇到符号“ SELECT”:(-+ case mod new not null继续avg计数当前存在最大值min先前的sql stddev总和方差执行forall合并时间时间戳记间隔日期管道<替代项

错误(528,3):PLS-00103:在预期以下情况之一时遇到了符号“ INNER”:对于具有相减负顺序的组,在连接处开始并入

任何人都可以告诉我我在哪里犯了一个错误,这是什么地方吗?

2 个答案:

答案 0 :(得分:0)

那是无效的语法。您可以执行此操作(如Florin Ghita在评论中建议的那样):

FUNCTION GET_MOMENT_PROJECT_USERS(p_busyUsersID IN INT)
   RETURN SYS_REFCURSOR IS 
    rc  SYS_REFCURSOR;
    /*getBusyUsersIDS*/
 BEGIN
  CASE bussyUserID WHEN bussyUserID THEN
    OPEN rc FOR
        SELECT u.*,
                ur.Roles 
        FROM 
                users u 
        INNER JOIN 
                user_roles ur 
        ON 
                u.User_roleID = ur.id 
        WHERE 
                u.UserID NOT IN (p_busyUsersID) 
        AND 
                u.Surname IS NOT NULL
        AND 
                u.Lastname IS NOT NULL 
        ORDER BY
                u.ProjectPlanSortIndex;

  ELSE  
     OPEN rc FOR
        SELECT  u.*, 
                ur.Roles 
        FROM 
                users u 
        INNER JOIN 
                user_roles ur 
        ON 
                u.User_roleID = ur.id 
        WHERE 
                u.Surname IS NOT NULL 
        AND 
                u.Lastname IS NOT NULL 
        ORDER BY 
                u.ProjectPlanSortIndex;
  END CASE;
  RETURN rc;
END GET_BUSY_USERS_IDS;

或者您可以将条件移至单个查询的WHERE子句中,例如:

FUNCTION GET_MOMENT_PROJECT_USERS(p_busyUsersID IN INT)
   RETURN SYS_REFCURSOR IS 
    rc  SYS_REFCURSOR;
    /*getBusyUsersIDS*/
 BEGIN
  CASE bussyUserID WHEN bussyUserID THEN
    OPEN rc FOR
        SELECT u.*,
                ur.Roles 
        FROM 
                users u 
        INNER JOIN 
                user_roles ur 
        ON 
                u.User_roleID = ur.id 
        WHERE 
                (bussyUserID != bussyUserID
                 OR u.UserID NOT IN (p_busyUsersID))
        AND 
                u.Surname IS NOT NULL
        AND 
                u.Lastname IS NOT NULL 
        ORDER BY
                u.ProjectPlanSortIndex;

  END CASE;
  RETURN rc;
END GET_BUSY_USERS_IDS;

但是,如果条件bussyUserID != bussyUserID的任一侧都可以为null,则在不进行任何处理以处理null的情况下将无法工作。

(注意:当您将bussyUserID与自身进行比较时,您的情况没有意义!)

答案 1 :(得分:0)

关于初学者如何处理这种语法问题,我建议您练习将其简化为最小的示例,以免被联接逻辑分散注意力。

例如,您可以快速对其进行测试,并发现它不起作用

declare
    rc sys_refcursor;
begin
    open rc for
        case
            when 1 = 1 then select * from dual  -- not valid
        end;
end;

失败

ORA-06550: line 6, column 29:
PLS-00103: Encountered the symbol "SELECT" when expecting one of the following: ...

因为open rc for select语法必须是单个语句,并且不能与条件逻辑分开。

请注意,没有create function,参数或联接等使事情复杂化的情况。弄清语法后,可以将所有这些重新添加。 case语句的语法为here。 (请注意,case有几种不同的类型。)它必须是

declare
    rc sys_refcursor;
begin
    case
        when 1 = 1 then
            open rc for select * from dual;
    end case;
end;

或使用if的等效项:

declare
    rc sys_refcursor;
begin
    if 1 = 1 then
        open rc for select * from dual;
    end if;
end;