2011-08-19 17 views
5

Pracuję z widokiem mysql i chcę użyć instrukcji IF ELSE w tym widoku. jego dając mi błąd jak tenmysql 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 

To jest mój widok

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 

) 

EDITED tutaj moja funkcja

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

Jeśli ktoś może mi pomóc dzięki

AKTUALIZACJA zapytanie

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) 

błąd 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

Sporządzono

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

jest 'getUser () "powinno być [CURRENT_USER() lub USER()] (http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_current-user)? –

+0

pytanie zaktualizowane –

+2

Ponadto, widok ma być normalną instrukcją SELECT, a te nie pozwalają na sterowanie przepływem bloków, tak jak robią to procedury przechowywane. Jako takie, będziesz chciał użyć [IF() lub CASE ... WHEN..END] (http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html) –

Odpowiedz

3

Jak skomentował widoczne, a View może posiadać prostą SELECT oświadczenie.

Można użyć pojedynczego zapytania z CASE bloku:

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 

Albo użyć kwerendy do przyłączenia w oparciu, (tworzenie 2nd widok obejść ograniczenie 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

błąd z informacją View's SELECT zawiera podzapytanie w klauzuli FROM –

+0

@roshan Zaktualizowany z obejściem –

+0

dzięki za odpowiedź wciąż problem i zaktualizowałem pytanie zobacz –

0

apostrofów!

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

Nie ma szczęścia: (..... –

+2

@Bohemian: MySQL [pozwala na zadeklarowanie stałych stałych za pomocą pojedynczych lub podwójnych cudzysłowów] (http://dev.mysql.com/doc/refman/5.0/en/string-syntax.html), chyba że włączysz opcję, która zezwala tylko na pojedyncze cudzysłowy. –