【问题标题】:MySQL DECLARE Syntax (Converting from MSSQL)MySQL DECLARE 语法(从 MSSQL 转换)
【发布时间】: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...
  • @dash,请用您的第一条评论“回答”以下问题,以便我接受,谢谢 :)

标签: mysql


【解决方案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

总是小事... :-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-22
    • 2015-07-25
    • 2018-08-09
    • 2013-12-01
    • 2014-01-04
    • 2017-12-07
    • 1970-01-01
    相关资源
    最近更新 更多