2011-08-19 13 views
5

मैं mysql दृश्य के साथ काम कर रहा हूं और मैं उस दृश्य पर IF ELSE कथन का उपयोग करना चाहता हूं। इसmysql देखें अगर अन्य समस्या

#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 

इस तरह अपनी मुझे दे त्रुटि मेरे विचार

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 

) 

यहाँ संपादित मेरी समारोह

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

धन्यवाद किसी भी एक मेरी मदद कर सकते हैं

अद्यतन किया जाता है प्रश्न

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) 

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

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) 'होना चाहिए [CURRENT_USER() या USER()] (http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_current-user)? –

+0

प्रश्न –

+2

अपडेट किया गया इसके अलावा, एक दृश्य सामान्य चयन कथन होना चाहिए, और वे संग्रहित प्रक्रियाओं जैसे ब्लॉक प्रवाह नियंत्रण की अनुमति नहीं देते हैं। इस प्रकार, आप [IF() या CASE का उपयोग करना चाहेंगे ... WHEN..END] (http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html) –

उत्तर

3

जितना कि टिप्पणी की, एक दृश्य एक सरल SELECT बयान कर सकते हैं।

आप CASE ब्लॉक के साथ एक एकल क्वेरी का उपयोग कर सकते हैं:

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 

या एक में शामिल होने के आधार पर क्वेरी, (MySql सीमा वैकल्पिक हल करने के लिए एक 2 दृश्य बनाने) का उपयोग करें: (

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

त्रुटि कह रही है दृश्य का चयन करें FROM क्लॉज –

+0

@roshan में एक सबक्वायरी है वर्कअराउंड –

+0

के साथ अपडेट किया गया उत्तर अभी भी समस्या के उत्तर के लिए धन्यवाद, मैंने प्रश्न अपडेट किया है कृपया –

0

एकल उद्धरण का उपयोग हो गया!

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

कोई भाग्य नहीं: (..... –

+2

@ बोहेमियन: MySQL [स्ट्रिंग स्थिरांक घोषित करने की अनुमति देता है एकल या दोहरे उद्धरणों का उपयोग करके] (http://dev.mysql.com/doc/refman/5.0/en/string-syntax.html), जब तक कि आप विशेष रूप से उस विकल्प को सक्षम नहीं करते हैं जो केवल एकल कोट्स को अनुमति देता है। –

संबंधित मुद्दे