2013-06-23 79 views
2

我試圖實現類似下面的東西,但我會錯誤的 - 與邏輯,語法一切。你能幫我麼?MySql案例函數內

我想根據一定條件SET STATIC =。如果這是PHP,我會用Break。我來到這裏,但沒有任何工作。你能看到如何做到這一點?

CREATE DEFINER=`root`@`localhost` FUNCTION `caseTest`(`n` INT) 
    RETURNS varchar(512) 
    LANGUAGE SQL 
    DETERMINISTIC 
    CONTAINS SQL 
    SQL SECURITY DEFINER 
    COMMENT '' 
BEGIN 

    DECLARE x VARCHAR(1500); 
    DECLARE y VARCHAR(1500); 
    DECLARE static VARCHAR(1500); 

    CASE n 
     WHEN '14' THEN SET x = 'Place Order Link'; 
     WHEN '01' THEN SET x = 'Cancel Order Link'; 
     WHEN '11' THEN SET x = 'Order Cancelled - Place order link'; 
     SET static = concat(<a href="">,x,</a>); 

     WHEN '00' THEN SET x = 'Order - Under Process'; #No link here 
     WHEN '10' THEN SET x = 'Cancel - Under Process'; #No link here 
     SET static = x; 

     ELSE SET static = 'Error generating link'; 
    END; 

set y = 'Flag Link ~ Edit Link ~ Move Link'; 

    RETURN concat(x,y); 
END 

回答

3

你要移動的setcase語句外:

set x = case n 
     WHEN '14' THEN 'Place Order Link'; 
     WHEN '01' THEN 'Cancel Order Link'; 
     WHEN '11' THEN 'Order Cancelled - Place order link'; 
     WHEN '00' THEN 'Order - Under Process'; #No link here 
     WHEN '10' THEN 'Cancel - Under Process'; #No link here 
     end; 
    set static = (case when n in ('14', '01', '11') then concat(<a href="">,x,</a>) 
        when n in ('00', '10') then x 
        ELSE 'Error generating link' 
       end); 

您還可以if做到這一點。 setcase似乎更接近您的原始邏輯。

我沒有看到上述不應該工作的原因(除了可能的語法錯誤)。您可以嘗試if版本:

if n = '14' THEN x = 'Place Order Link'; 
elseif n = '01' THEN x = 'Cancel Order Link'; 
elseif n = '11' THEN x = 'Order Cancelled - Place order link'; 
elseif n = '00' THEN x = 'Order - Under Process'; #No link here 
elseif n = '10' THEN x = 'Cancel - Under Process'; #No link here 

if n in ('14', '01', '11') then static = concat(<a href="">,x,</a>); 
elseif n in ('00', '10') then static = x; 
ELSE static = 'Error generating link'; 
+0

MySql語法是否因操作系統而異?我在WAMP上,並且我正在凝固時間讓這個保存。我正在使用Heidi Sql。 – Norman