如何在 MySQL 过程中获取 ENUM 的值
在我看来,枚举在 MySQL 中被破坏了,至少在版本 5.0.51a-24+lenny5 (Debian) 上是这样。我有一个简单的查找表,其中包含一个枚举字段:
CREATE TABLE `FrameworkServices` (
...
`DNSType` enum('IP','CNAME') NOT NULL,
...
)
临时,我可以毫无问题地运行以下命令:
SELECT DNSType, CNameOrIP INTO @hackEnum, @ipAddress
FROM FrameworkServices WHERE FrameworkTypeId = 1 LIMIT 1;
SELECT @hackEnum, @ipAddress;
但是,如果我将完全相同的代码放入过程中,@hackEnum 返回空(不为空,至少通过 TOAD) 。
我尝试选择 DNSType+0、CONCAT(DNSType,'') 以及其他似乎对枚举有效的内容,结果是相同的: 我阅读的每一篇文章都是临时的,但在程序中返回空!
我什至尝试使用局部变量并将 hackEnum 定义为相同的 ENUM 类型。
It seems to me that enums are broken within MySQL, at least on version 5.0.51a-24+lenny5 (Debian). I have a simple lookup table which contains an enum field:
CREATE TABLE `FrameworkServices` (
...
`DNSType` enum('IP','CNAME') NOT NULL,
...
)
Ad-hoc, I can run the following without issue:
SELECT DNSType, CNameOrIP INTO @hackEnum, @ipAddress
FROM FrameworkServices WHERE FrameworkTypeId = 1 LIMIT 1;
SELECT @hackEnum, @ipAddress;
However, if I put the EXACT same code into a Procedure, @hackEnum returns empty (not null, at least through TOAD).
I have tried selecting DNSType+0, CONCAT(DNSType,'') and everything else that seems to be valid on enums and the result is the same:
EVERY article I read works ad-hoc, but returns empty within the Procedure!
I've even tried using local variables and defining hackEnum as the same ENUM type.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了完全相同的问题 - 我正在检索 ENUM 值,然后通过 IF...ELSE...END IF 检查它。它不起作用,让我抓狂,不知道为什么。解决方案是选择一个 char(1) ,然后将 ENUM 列值视为字符串:
我首先使用:
虽然技术上是正确的,但失败了。希望这对您有帮助。
I had the exact same problem - I was retrieving an ENUM value and then checking it via
IF...ELSE...END IF
. It wasn't working and driving me nuts as to why not. The solution was to select into a char(1) and then treat the ENUM column value as a string:I first used:
and while technically correct, failed. Hope this helps you.
只声明你的返回变量,有 varchar(xx);
这一切!
Only declare your variable of return, has varchar(xx);
and this all!!