错误创建存储过程 MySQL

Posted

技术标签:

【中文标题】错误创建存储过程 MySQL【英文标题】:ERROR Creating Stored Procedure MySQL 【发布时间】:2014-05-07 09:03:05 【问题描述】:

我想使用 phpmyadmin 在 mysql 数据库中创建一个存储过程。

几分钟后,我在“创建过程”对话框中输入并按 GO,我的语法出现错误。

我在谷歌上搜索了哪个是错误的,但仍然没有找到。请任何人给我一些建议如何解决这个问题。

这是我的“MySQL”存储过程查询。

DELIMITER $$
BEGIN
IF cek=1 THEN
    SELECT a.NIK AS NIK
            , a.EmployeeName AS EmployeeName
            , a.Position AS Position
            , b.TypeName AS EmployeeType
            , TIME(c.EventTime) AS Datang
            , (CASE WHEN a.TypeID=3 THEN 
                    (CASE WHEN TIME(c.EventTime)>(SELECT TimeIn FROM MST_Shift 
                    WHERE ShiftID=(SELECT ShiftID FROM SCH_Shifting WHERE NIK=a.NIK AND DATE(ShiftDate)=DATE(sdate)
                    AND IsActive=1 AND IsDeleted=0))
                    THEN 'Late' ELSE 'On Time' END)
              WHEN a.TypeID=1 OR a.TypeID=2 THEN
                    (CASE WHEN TIME(c.EventTime)>'08:15:00' THEN 'Late' ELSE 'On Time' END)
              WHEN a.TypeID=6 OR a.TypeID=7 THEN
                    (CASE WHEN TIME(c.EventTime)>(SELECT TimeIn FROM MST_Shift
                    WHERE TypeID=a.TypeID LIMIT 1) THEN 'Late' ELSE 'On Time' END)
              WHEN a.TypeID=5 THEN
                    (CASE WHEN TIME(c.EventTime)>(SELECT TimeIn FROM MST_Shift
                    WHERE ShiftID=(CASE WHEN DAYOFWEEK(sdate)=7 THEN 12 ELSE 11 END)) THEN 'Late' ELSE 'OnTime' END)
              WHEN a.TypeID=4 THEN
                    (CASE WHEN TIME(c.EventTime)>(SELECT TimeIn FROM SCH_Special
                    WHERE NIK=a.NIK AND DATE(AbsenceDate)=DATE(sdate) AND IsDeleted=0) THEN 'Late' ELSE 'OnTime' END)
              ELSE 'Undefined' END) AS Description
    FROM MST_Employee a
    INNER JOIN MST_EmployeeType b ON b.TypeID=a.TypeID
    INNER JOIN VW_AttendanceIN c ON a.NIK=c.NIK
    WHERE DATE(c.EventTime)=DATE(sdate)
    GROUP BY b.TypeName
            , a.Position
            , a.EmployeeName;
ELSE
    SELECT a.NIK AS NIK
            , a.EmployeeName AS EmployeeName
            , a.Position AS Posisi
            , b.TypeName AS Tipe
            , TIME(c.EventTime) AS Datang
            , (CASE WHEN a.TypeID=3 THEN 
                    (CASE WHEN TIME(c.EventTime)>(SELECT TimeIn FROM MST_Shift 
                    WHERE ShiftID=(SELECT ShiftID FROM SCH_Shifting WHERE NIK=a.NIK AND DATE(ShiftDate)=DATE(sdate)
                    AND IsActive=1 AND IsDeleted=0))
                    THEN 'Late' ELSE 'On Time' END)
              WHEN a.TypeID=1 OR a.TypeID=2 THEN
                    (CASE WHEN TIME(c.EventTime)>'08:15:00' THEN 'Late' ELSE 'On Time' END)
              WHEN a.TypeID=6 OR a.TypeID=7 THEN
                    (CASE WHEN TIME(c.EventTime)>(SELECT TimeIn FROM MST_Shift
                    WHERE TypeID=a.TypeID LIMIT 1) THEN 'Late' ELSE 'On Time' END)
              WHEN a.TypeID=5 THEN
                    (CASE WHEN TIME(c.EventTime)>(SELECT TimeIn FROM MST_Shift
                    WHERE ShiftID=(CASE WHEN DAYOFWEEK(sdate)=7 THEN 12 ELSE 11 END)) THEN 'Late' ELSE 'OnTime' END)
              WHEN a.TypeID=4 THEN
                    (CASE WHEN TIME(c.EventTime)>(SELECT TimeIn FROM SCH_Special
                    WHERE NIK=a.NIK AND DATE(AbsenceDate)=DATE(sdate) AND IsDeleted=0) THEN 'Late' ELSE 'OnTime' END)
              ELSE 'Undefined' END) AS Description
    FROM MST_Employee a
    INNER JOIN MST_EmployeeType b ON b.TypeID=a.TypeID
    INNER JOIN VW_AttendanceIN c ON a.NIK=c.NIK
    WHERE DATE(c.EventTime)=DATE(sdate)
    AND b.TypeID=type
    AND (a.NIK LIKE '%'+search+'%' OR a.EmployeeName LIKE '%'+key+'%')
    GROUP BY b.TypeName
            , a.Position
            , a.EmployeeName;
END IF;
END$$

我遇到了这样的错误

处理您的请求:

以下查询失败:“CREATE DEFINER=root@%PROCEDURE USP_SelectAttendance(IN cek BIT(1), IN type INT, IN sdate DATETIME, IN search VARCHAR(100)) NOT DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER .......

MySQL 说:#1064 - 你的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“$$ BEGIN IF cek=1 THEN SELECT a.NIK AS NIK, a.EmployeeName AS”附近使用正确的语法

此外,当我在 SQL 中运行“SELECT”查询时,它可以正常工作。

错误出现在创建存储过程时。

PS:对不起,英语不好

【问题讨论】:

如果您收到语法错误消息,请在此处发布。 Mike W:我已经发布了错误信息 【参考方案1】:

变化:

...
AND (a.NIK LIKE '%'+search+'%' OR a.EmployeeName LIKE '%'+key+'%')
...

作者:

...
AND (a.NIK LIKE CONCAT('%',search,'%') OR a.EmployeeName LIKE CONCAT('%',`key`,'%'))
...

SQL Fiddle demo

【讨论】:

以上是关于错误创建存储过程 MySQL的主要内容,如果未能解决你的问题,请参考以下文章

Mysql语法错误创建存储过程

错误 1604 创建 MySQL 存储过程。 MySQL 版本 5.7.19

Mysql创建存储过程时出现ERROR 1064错误

MySql存储过程调用错误

用Navicat连接MySQL创建存储过程时提示1304错误是为啥?r并不存在呀

MySQL存储过程语法错误:if else