2011-08-19 17 views
5

Sto lavorando con mysql view e voglio usare l'istruzione IF ELSE su quella vista. la sua dandomi errore come questomysql view if else problem

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'if(getUser()="") THEN] 
     select hie_code_1 from hs_hr_emp_level L,hs_hr_u' at line 7 

Questa è la mia vista

drop view if exists vw_hs_hr_employee; 

CREATE VIEW vw_hs_hr_employee as 
select * from hs_hr_employee where 
hie_code_1 in 
(
BEGIN 

    if(getUser()="") THEN 
     select hie_code_1 from hs_hr_emp_level L 
    ELSE 
      select hie_code_1 from hs_hr_emp_level L,hs_hr_users U 
      where L.emp_number=U.emp_number 
       and L.emp_number=getUser() 
       and (U.def_level=1 or U.def_level=4) 
    END if 

) 

CURA qui la mia funzione

CREATE FUNCTION `getUser`() RETURNS char(50) CHARSET latin1 
RETURN @user 

Se qualcuno mi può aiutare a grazie

AGGIORNATO query

CREATE VIEW vw_hs_hr_employee as 
select * from hs_hr_employee where 
CASE getUser() 
     WHEN '' 
    THEN 
    select hie_code_1 from hs_hr_emp_level L 
    END 
hie_code_1 in (select hie_code_1 from hs_hr_emp_level L,hs_hr_users U where L.emp_number=U.emp_number and L.emp_number=getUser() and (U.def_level=1 or U.def_level=4) ) 
or 
hie_code_3 in (select hie_code_3 from hs_hr_emp_level L,hs_hr_users U where L.emp_number=U.emp_number and L.emp_number=getUser() and U.def_level=2 ) 
or 
    hie_code_4 in (select hie_code_4 from hs_hr_emp_level L,hs_hr_users U where L.emp_number=U.emp_number and L.emp_number=getUser() and U.def_level=3) 

errore givinign syntax to use near 'select hie_code_1 from hs_hr_emp_level L END hie_code_1 in (select hie_code_' at line 6

Fatto

drop view if exists vw_hs_hr_employee; 
CREATE VIEW vw_hs_hr_employee as 
select * from hs_hr_employee e where CASE WHEN getUser()='' 
    THEN 
    e.emp_number is not null 
    ELSE 
hie_code_1 in (select hie_code_1 from hs_hr_emp_level L,hs_hr_users U where L.emp_number=U.emp_number and L.emp_number=getUser() and (U.def_level=1 or U.def_level=4) ) 
or 
hie_code_3 in (select hie_code_3 from hs_hr_emp_level L,hs_hr_users U where L.emp_number=U.emp_number and L.emp_number=getUser() and U.def_level=2 ) 
or 
    hie_code_4 in (select hie_code_4 from hs_hr_emp_level L,hs_hr_users U where L.emp_number=U.emp_number and L.emp_number=getUser() and U.def_level=3) 

end 
+0

è 'getUser () "supposto essere [CURRENT_USER() o USER()] (http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_current-user)? –

+0

domanda aggiornata –

+2

Inoltre, una vista dovrebbe essere una normale istruzione SELECT, e quelli non consentono il controllo del flusso dei blocchi come fanno le stored procedure. Pertanto, ti consigliamo di utilizzare [IF() o CASE ... WHEN..END] (http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html) –

risposta

3

Come ha commentato cospicuo, una visualizzazione può contenere una semplice SELECT dichiarazione.

È possibile utilizzare una singola query con CASE blocco:

CREATE VIEW vw_hs_hr_employee as 
SELECT * 
FROM hs_hr_employee 
WHERE CASE WHEN getUser() = '' 
    THEN hie_code_1 IN (
     SELECT hie_code_1 
     FROM hs_hr_emp_level) 
    ELSE hie_code_1 IN (
     SELECT hie_code_1 
     FROM hs_hr_emp_level L,hs_hr_users U 
      WHERE L.emp_number=U.emp_number 
       AND L.emp_number=getUser() 
       AND (U.def_level=1 or U.def_level=4)) 
    END 

Oppure utilizzare una query basata aderire, (la creazione di un secondo al fine di aggirare la limitazione MySql):

DROP VIEW IF EXISTS vw_hs_hr_employee_sub; 

CREATE VIEW vw_hs_hr_employee_sub AS 
SELECT hie_code_1 
FROM hs_hr_emp_level L 
    LEFT JOIN hs_hr_users U 
     ON L.emp_number = U.emp_number 
     AND L.emp_number = getUser() 
     AND (U.def_level=1 or U.def_level=4) 
WHERE getUser() = '' OR U.emp_number IS NOT NULL 
GROUP BY 1; 


drop view if exists vw_hs_hr_employee; 

CREATE VIEW vw_hs_hr_employee as 
SELECT e.* 
FROM hs_hr_employee e JOIN vw_hs_hr_employee_sub USING(hie_code_1) 
+0

errore dire View's SELECT contiene una sottoquery nella clausola FROM –

+0

@roshan Aggiornato con soluzione alternativa –

+0

grazie per la risposta ancora in corso di rilascio ho aggiornato la domanda si prega di vedere –

0

Usa virgolette singole!

... 
if(getUser()='') THEN 
... 
+0

Nessuna fortuna: (..... –

+2

@Bohemian: MySQL [consente di dichiarare costanti stringa utilizzando virgolette singole o doppie] (http://dev.mysql.com/doc/refman/5.0/en/string-syntax.html), a meno che non si abiliti specificamente l'opzione che consente solo virgolette singole. –