2013-03-13 16 views
5

Sto lavorando con una query che contiene istruzioni "IF" all'interno di una clausola "WHERE". Ma PL \ SQL Developer sta dando alcuni errori durante l'esecuzione. Qualcuno può aiutarmi con la query corretta? Ecco la query:If statement within Where clausola

SELECT t.first_name, 
     t.last_name, 
     t.employid, 
     t.status 
    FROM employeetable t 
WHERE IF status_flag = STATUS_ACTIVE then t.status = 'A' 
     IF status_flag = STATUS_INACTIVE then t.status = 'T' 
     IF source_flag = SOURCE_FUNCTION then t.business_unit = 'production' 
     IF source_flag = SOURCE_USER then t.business_unit = 'users' 
    AND t.first_name LIKE firstname 
    AND t.last_name LIKE lastname 
    AND t.employid LIKE employeeid; 

Ho ricevuto l'errore "ORA-00920: operatore relazionale non valido".

parentesi Pubblica intorno status_flag = STATUS_ACTIVE risultati in errore "ORA-00907: mancano parentesi destra"

risposta

9

CASE potrebbe dare una mano:

SELECT t.first_name, 
     t.last_name, 
     t.employid, 
     t.status 
    FROM employeetable t 
WHERE t.status = (CASE WHEN status_flag = STATUS_ACTIVE THEN 'A' 
         WHEN status_flag = STATUS_INACTIVE THEN 'T' 
         ELSE null END) 
    AND t.business_unit = (CASE WHEN source_flag = SOURCE_FUNCTION THEN 'production' 
           WHEN source_flag = SOURCE_USER THEN 'users' 
           ELSE null END) 
    AND t.first_name LIKE firstname 
    AND t.last_name LIKE lastname 
    AND t.employid LIKE employeeid; 

Il CASE statement valuta le più condizioni per produrre un singolo valore. Quindi, nel primo utilizzo, controllo il valore di status_flag, restituendo 'A', 'T' o null a seconda del suo valore e confrontandolo con t.status. Faccio lo stesso per la colonna business_unit con una seconda dichiarazione CASE.

+0

grazie! questo ha davvero aiutato – user2100620

+0

@DCookie, Come posso aggiungere una condizione della clausola WHERE all'interno di un'istruzione IF? – masT

+0

Caso: 't.status = null' non funzionerebbe per i casi in cui t.status è effettivamente nullo. Null è strano. – defactodeity

3

Non si può usare se così. Puoi fare quello che vuoi con AND e OR:

SELECT t.first_name, 
     t.last_name, 
     t.employid, 
     t.status 
    FROM employeetable t 
WHERE (status_flag = STATUS_ACTIVE AND t.status = 'A' 
    OR status_flag = STATUS_INACTIVE AND t.status = 'T' 
    OR source_flag = SOURCE_FUNCTION AND t.business_unit = 'production' 
    OR source_flag = SOURCE_USER  AND t.business_unit = 'users') 
    AND t.first_name LIKE firstname 
    AND t.last_name LIKE lastname 
    AND t.employid LIKE employeeid; 
+0

Invece di utilizzare AND e OR, cosa succede se ho usato CASE? 'DOVE CASO quando status_flag = STATUS_ACTIVE poi t.status = 'A' quando status_flag = STATUS_INACTIVE poi t.status = 'T' quando source_flag = SOURCE_FUNCTION poi t.business_unit = 'produzione' quando source_flag = SOURCE_USER poi t.business_unit = 'utenti' FINE E t.first_name cognome COME E t.last_name COME cognome E t.employid COME employeeid; ' ottengo l'errore "ORA-00905: parola chiave mancante". Che cosa esattamente ho fatto di sbagliato in questo caso? – user2100620

+0

Vedi la mia risposta. Ti stai avvicinando. – DCookie