简体   繁体   中英

MySql concat when using case inside a function

How can I concat a fixed string to text that is dynamically generated in the case below? I've kept it simple for this example, but the real thing is a lot. So instead of having it all over the place, I'd like to concat it and send it off to the output.

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

Is this what you want?

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'

In your code snippet, mStatus is not defined. I assume that in the original code, this is taken care of.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM