2

我正在尝试实现如下所示的目标,但我错了——逻辑、语法和其他一切。你能帮我么?

我正在尝试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
4

1 回答 1

3

你想把语句移到set外面:case

  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. setwithcase似乎更接近您的原始逻辑。

我看不出上面为什么不起作用(除了可能的语法错误)。你可以试试这个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';
于 2013-06-23T16:14:19.947 回答