MySQL - CREATE DEFINER语法错误

时间:2016-11-07 14:44:20

标签: mysql

我正在尝试更新MySQL数据库中的存储函数。更新将发布到多个设备,因此我通过update.sql文件进行更新。

这是函数

SELECT RS.[ScheduleID]
      , C.Name
      ,[ReportID]
      ,RS.[SubscriptionID]
      ,[ReportAction]
      ,S.LastRunTime
      , SC.*
  FROM [ReportServer].[dbo].[ReportSchedule] RS
  JOIN ReportServer.dbo.Catalog C on RS.ReportID = C.ItemID
  JOIN msdb.dbo.sysjobs sj on Cast(RS.ScheduleID as varchar(500)) = cast(sj.name as varchar(500))
  JOIN ReportServer.dbo.Subscriptions S ON RS.SubscriptionID = S.SubscriptionID
  JOIN ReportServer.dbo.Schedule SC ON RS.ScheduleID = SC.ScheduleID

测试团队刚刚向我报告使用此功能的报告未正确生成。我试图在PMA SQL查询窗口中运行代码并获得以下内容:

DROP FUNCTION `STAFF_MPT`;
CREATE DEFINER=`jelena`@`%` FUNCTION `STAFF_MPT`(`par_stocktake_staff_id`     INT) RETURNS DECIMAL(20,0) NOT DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER BEGIN

DECLARE proc_total INT;
DECLARE proc_time INT;

SET proc_total = (SELECT SUM(quantity) FROM stocktake_scans WHERE stocktake_staff_id = par_stocktake_staff_id);

SET proc_time = (SELECT TIMESTAMPDIFF( SECOND , MIN( scan_date ) , MAX( scan_date ) ) AS area_time
    FROM stocktake_scans
    WHERE stocktake_staff_id = par_stocktake_staff_id
    );

RETURN (proc_total/proc_time)*3600;

END

有人能告诉我我错过了什么吗?根据这个,第3行是空的,那怎么可能有语法错误?

1 个答案:

答案 0 :(得分:2)

截至MySQL docs

  

如果使用mysql客户端程序定义包含分号字符的存储程序,则会出现问题。默认情况下,mysql本身将分号识别为语句分隔符,因此必须临时重新定义分隔符以使mysql将整个存储的程序定义传递给服务器。

     

要重新定义mysql分隔符,请使用delimiter命令。

@juergen_d在评论中暗示:你必须用分隔符定义你的程序:

DROP FUNCTION `STAFF_MPT`;

delimiter ||

CREATE DEFINER=`jelena`@`%` FUNCTION `STAFF_MPT`(`par_stocktake_staff_id`     INT) RETURNS DECIMAL(20,0) NOT DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER BEGIN

DECLARE proc_total INT;
DECLARE proc_time INT;

SET proc_total = (SELECT SUM(quantity) FROM stocktake_scans WHERE stocktake_staff_id = par_stocktake_staff_id);

SET proc_time = (SELECT TIMESTAMPDIFF( SECOND , MIN( scan_date ) , MAX( scan_date ) ) AS area_time
    FROM stocktake_scans
    WHERE stocktake_staff_id = par_stocktake_staff_id
    );

RETURN (proc_total/proc_time)*3600;

END

||
delimiter ;