MySQL DECLARE语法(从MSSQL转换)

时间:2011-12-14 00:16:30

标签: mysql

MySQL Gurus,

我正在转换MSSQL数据库中的一些报告,以便在MySQL数据库上使用,似乎并不了解DECLARE在MySQL中是如何工作的。下面是报告的SQL代码,在MSSQL中可以使用。我读到DECLARE只能在嵌套函数中使用,我相信,但这对我来说听起来不对。

当前报告SQL :(我从我的应用代码中解析并替换当前和待定的值)

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)

1 个答案:

答案 0 :(得分:1)

您可以单独使用SET(无DECLARE)或将@替换为_或类似(甚至不使用前缀)。

我通常用_

作为我的前缀

有关类似问题,请参阅What's wrong with this MySQL statement: DECLARE @ID INT

关于你的其他评论,它是MySql中的IFNULL - 见http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_ifnull

总是小事......: - )

相关问题