在函数内部使用case时MySql concat

时间:2013-06-23 18:17:55

标签: mysql sql

在下面的案例中,我如何concat fixed string发送dynamically generated的文字?我为这个例子保持简单,但真实的东西很多。因此,我不想将它全部放在一起,而是concat它并将其发送到输出。

BEGIN
    DECLARE aId INT;
    DECLARE aType INT;
    DECLARE aParent INT;
    DECLARE aUserName VARCHAR(32);
    DECLARE aUserId INT;
    DECLARE aCountry VARCHAR(2);
    DECLARE aOutput VARCHAR(1500); 

    SELECT id, type, parent INTO aId, aType, aParent FROM products  WHERE id = mElmId;

    SET aOutput = CASE atype
        WHEN 1 THEN 'Something'
        WHEN 2 THEN 'Something'
        WHEN 3 THEN  'Something'
        WHEN 10 THEN  
            CASE mStatus
                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
                ELSE 'Detect Status Error'
            END
            //I need to concat 'Home ~ More' to the above text, but don't want to add it next to the text above.
            //So it ends up like 'Place Order Link ~ Home ~More'
        ELSE 'Error generating link'
    END;

  RETURN (aOutput);
END

1 个答案:

答案 0 :(得分:2)

这是你想要的吗?

SET aOutput = CASE atype
    WHEN 1 THEN 'Something'
    WHEN 2 THEN 'Something'
    WHEN 3 THEN  'Something'
    WHEN 10 THEN  
        concat(CASE mStatus
                  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
                  ELSE 'Detect Status Error'
              END, ' Home ~ More')
        //I need to concat 'Home ~ More' to the above text, but don't want to add it next to the text above.
        //So it ends up like 'Place Order Link ~ Home ~More'
    ELSE 'Error generating link'

在您的代码段中,未定义mStatus。我假设在原始代码中,这是照顾的。