Estoy trabajando con la vista de mysql y quiero usar la instrucción IF ELSE en esa vista. me facilita error como estemysql ver si no problema
#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
Este es mi punto de 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
)
EDITADO aquí mi función
CREATE FUNCTION `getUser`() RETURNS char(50) CHARSET latin1
RETURN @user
Si alguno me puede ayudar gracias
actualiza consulta
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)
error 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
Hecho
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
es 'getUser () 'se supone que es [CURRENT_USER() o USER()] (http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_current-user)? –
pregunta actualizada –
Además, se supone que una vista es una instrucción SELECT normal, y esas no permiten el control de flujo de bloque como lo hacen los procedimientos almacenados. Como tal, querrá usar [IF() o CASE ... WHEN..END] (http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html) –