CASE in MySQL forces column datatype to BIGINT instead of INT

烂漫一生 提交于 2019-12-11 10:00:23

问题


Well it would rather be a very simple thing but I couldn't figure out myself.

While trying the below line in Stored Procedure in MYSQL server, it converts or sets the column datatype to BIGINT instead of expected INT

CASE `L`.`Code` WHEN 'ABCD' THEN 0 ELSE `XL`.`ID` END as XLID

The magical thing is that if I remove the 0 from it, then it returns INT as datatype as expected. So this 0 is to blame for sure, but I couldn't understand why and how to resolve this? I tried to use CAST but it didn't allow to cast INT to INT.

Would be a great help.


回答1:


The following trick can help:

DELIMITER //

CREATE FUNCTION `return_integer`(`number` INT UNSIGNED)
RETURNS INT UNSIGNED DETERMINISTIC
BEGIN
    RETURN `number`;
END//

DELIMITER ;

SELECT
    CASE `L`.`Code`
       WHEN 'ABCD' THEN `return_integer`(0)
       ELSE `L`.`ID`
    END AS XLID
FROM `table_name` `L`;


来源:https://stackoverflow.com/questions/27907817/case-in-mysql-forces-column-datatype-to-bigint-instead-of-int

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!