2012-11-04 12 views
20

Sto provando a stampare il TESTO quando la condizione è VERO. Il codice selezionato funziona perfettamente. Sta mostrando il valore 403 quando eseguo solo un codice selezionato. Ma devo stampare del testo quando esiste una condizione. Qual è il problema con il seguente codice.Condizione IF EXISTS che non funziona con PLSQL

BEGIN 
IF EXISTS(
SELECT CE.S_REGNO FROM 
COURSEOFFERING CO 
JOIN CO_ENROLMENT CE 
    ON CE.CO_ID = CO.CO_ID 
WHERE CE.S_REGNO=403 AND CE.COE_COMPLETIONSTATUS = 'C' AND CO.C_ID = 803 
) 
THEN 
    DBMS_OUTPUT.put_line('YES YOU CAN'); 
END; 

Ecco il report di errore:

Error report: 
ORA-06550: line 5, column 1: 
PLS-00103: Encountered the symbol "JOIN" when expecting one of the following: 

    ) , with group having intersect minus start union where 
    connect 
06550. 00000 - "line %s, column %s:\n%s" 
*Cause: Usually a PL/SQL compilation error. 
*Action: 

risposta

38

IF EXISTS() è semanticamente corretto. La condizione EXISTS può essere utilizzata solo all'interno di un'istruzione SQL. Così si potrebbe riscrivere il blocco PL/SQL come segue:

declare 
    l_exst number(1); 
begin 
    select case 
      when exists(select ce.s_regno 
         from courseoffering co 
         join co_enrolment ce 
          on ce.co_id = co.co_id 
         where ce.s_regno=403 
          and ce.coe_completionstatus = 'C' 
          and ce.c_id = 803 
          and rownum = 1 
         ) 
      then 1 
      else 0 
     end into l_exst 
    from dual; 

    if l_exst = 1 
    then 
    DBMS_OUTPUT.put_line('YES YOU CAN'); 
    else 
    DBMS_OUTPUT.put_line('YOU CANNOT'); 
    end if; 
end; 

Oppure si può semplicemente utilizzare count funzione di non determinare il numero di righe restituite dalla query, e rownum=1 predicato - avete solo bisogno di sapere se esiste un record :

declare 
    l_exst number; 
begin 
    select count(*) 
    into l_exst 
    from courseoffering co 
      join co_enrolment ce 
      on ce.co_id = co.co_id 
    where ce.s_regno=403 
     and ce.coe_completionstatus = 'C' 
     and ce.c_id = 803 
     and rownum = 1; 

    if l_exst = 0 
    then 
    DBMS_OUTPUT.put_line('YOU CANNOT'); 
    else 
    DBMS_OUTPUT.put_line('YES YOU CAN'); 
    end if; 
end; 
+0

Grazie Nicholas per il tuo codice geniale. Funziona perfettamente per me. – nirmalgyanwali

+0

"usa semplicemente la funzione di conteggio" è molto meno efficiente. Rimanere con esiste/non esiste. – miraclefoxx

+0

Perché 'exists' non può essere utilizzato nelle istruzioni di inserimento? – zygimantus

5

Purtroppo PL/SQL non hai IF EXISTS operatore come SQL Server. Ma puoi fare qualcosa del genere:

begin 
    for x in (select count(*) cnt 
       from dual 
       where exists (
       select 1 from courseoffering co 
        join co_enrolment ce on ce.co_id = co.co_id 
       where ce.s_regno = 403 
        and ce.coe_completionstatus = 'C' 
        and co.c_id = 803)) 
    loop 
     if (x.cnt = 1) 
     then 
      dbms_output.put_line('exists'); 
     else 
      dbms_output.put_line('does not exist'); 
     end if; 
    end loop; 
end; 
/
+0

Grazie Hilton per il tuo aiuto. Ya, ho avuto l'idea dal tuo codice. Ma non so perché la condizione sia FALSE e sta mostrando la condizione ELSE. Il valore in x.cnt è 0. – nirmalgyanwali

+0

Probabilmente ciò è dovuto al fatto che ho ridotto la query con la costante "C", che dovrebbe essere in maiuscolo. – HiltoN

Problemi correlati