2016-12-15 488 views
2

子查詢我有如下圖所示TRUNC(日期)與甲骨文

IF (TRUNC(lv_d_itr) IN 
(SELECT next_day(TRUNC(to_date('01-01-17','dd-mm-yy'),'YYYY') + 14*(level-1), 'WED')+7 
FROM dual 
CONNECT BY level <= 26)) THEN 

,檢查在子查詢日期的IF條件爲什麼我會得到下面的錯誤?用法是錯誤的?

PLS-00405: subquery not allowed in this context 

什麼可能是替代解決方案?

回答

1

您可以用不同的方式編輯這個,一個簡單的可以是這樣的:

declare 
    n number; 
begin 
    select count(1) 
    into n 
    from 
     (  
     SELECT next_day(TRUNC(to_date('01-01-17','dd-mm-yy'),'YYYY') + 14*(level-1), 'MER')+7 as x 
     FROM dual 
     CONNECT BY level <= 26 
     ) 
    where x = TRUNC(lv_d_itr); 
    if n > 0 
     then ... 
    end if; 
    ... 
end; 
+0

謝謝Aleksej。所以我應該檢查'IF 1 = *上面提到的代碼*'?我對嗎? – Crazy2crack

+0

@ Crazy2crack:看我的編輯;我希望它很清楚 – Aleksej

3

不能使用在PL/SQL IF語句子查詢:

BEGIN 
    IF 'X' IN (SELECT DUMMY FROM DUAL) THEN 
    DBMS_OUTPUT.PUT_LINE('X'); 
    END IF; 
END; 
/

威爾錯誤與PLS-00405: subquery not allowed in this context

您可以重構它以獲得相同的效果:

DECLARE 
    p_exists NUMBER; 
BEGIN 
    SELECT CASE WHEN 'X' IN (SELECT DUMMY FROM DUAL) 
       THEN 1 
       ELSE 0 
     END 
    INTO p_exists 
    FROM DUAL; 

    IF p_exists = 1 THEN 
    DBMS_OUTPUT.PUT_LINE('X'); 
    END IF; 
END; 
/
+0

即使使用'CASE'語句也會給出相同的錯誤。 – Crazy2crack