【发布时间】:2011-12-14 08:36:19
【问题描述】:
MySQL 大师,
我正在转换 MSSQL 数据库中的一些报告以在 MySQL 数据库上使用,但似乎不明白 DECLARE 在 MySQL 中的工作原理。下面是报告的 SQL 代码,与 MSSQL 中的一样。我读到 DECLARE 只能在嵌套函数中使用,我相信,但这对我来说听起来不对。
当前报告 SQL:(我从我的应用代码中解析并替换 Current 和 Pending 的值)
DECLARE @Current int;
DECLARE @Pending int;
SET @Current = [1];
SET @Pending = [3];
Select Ticket.TIcketID,
ISNULL((Select LocationName from Location where LocationID = Ticket.SiteCurrentLocation), 'Invalid Location') as [Current Location],
ISNULL((Select LocationName from Location where LocationID = Ticket.SitePendingLocation), 'Invalid Location') as [Pending Location]
from Ticket
where
(SitePendingLocation > 0 AND SitePendingLocation <> SiteCurrentLocation) AND
(SiteCurrentLocation = @Current OR @Current = 0) AND
(SitePendingLocation = @Pending OR @Current = 0)
有什么见解吗?
谢谢 - 安德鲁
编辑
工作,转换后的脚本 - 它可以帮助其他人:
SET @Current = '1';
SET @Pending = '1';
Select Ticket.TIcketID,
IFNULL((Select LocationName from Location where LocationID = Ticket.SiteCurrentLocation), 'Invalid Location') as `Current Location`,
IFNULL((Select LocationName from Location where LocationID = Ticket.SitePendingLocation), 'Invalid Location') as `Pending Location`
from Ticket
where
(SitePendingLocation > 0 AND SitePendingLocation <> SiteCurrentLocation) AND
(SiteCurrentLocation = @Current OR @Current = 0) AND
(SitePendingLocation = @Pending OR @Current = 0)
【问题讨论】:
-
您可以单独使用 SET(无 DECLARE)或将 @ 替换为 _(或无前缀)。有关类似问题,请参阅 stackoverflow.com/questions/763718/…。
-
谢谢@dash - 那时根本不需要
DECLARE,太好了!现在开始弄清楚它不喜欢ISNULL... -
这是 IFNULL ;-) - dev.mysql.com/doc/refman/5.0/en/…
-
@dash,请用您的第一条评论“回答”以下问题,以便我接受,谢谢 :)
标签: mysql