mysql custom global defined variable

前端 未结 4 1588
逝去的感伤
逝去的感伤 2021-01-21 00:10

In my database design, I tend to store some variable that is meant to be acting as a ROLE or TYPE as SMALLINT. For example :



        
4条回答
  •  太阳男子
    2021-01-21 01:03

    IMHO, MySQL has a huge gap in this area, apparently in the latter versions. One alternative might have been to resort to setting OS environment variables, but how such values can be retrieved from within MySQL, I've been unable to see.

    There's a whole page here: https://dev.mysql.com/doc/refman/5.0/en/setting-environment-variables.html teaching us how to "set" OS environment variables in the shell, but not a word on actually calling such variables in MySQL.

    As another workaround, using a FUNCTION might be considered more lightweight than a STORED PROCEDURE, like so:

    CREATE DEFINER=`root`@`localhost` FUNCTION `DEFAULT_COUNTRY_CODE`() RETURNS CHAR(4)
    DETERMINISTIC
    RETURN '+234';
    

    Elsewhere in your query, you can then do:

    SELECT CONCAT(DEFAULT_COUNTRY_CODE(), "-", telephone) FROM contacts WHERE CountryCode = "NGA"
    

提交回复
热议问题