title: ERROR Creating Stored Procedure MySQL
body: <p>I want to make a stored procedure in MySQL database using phpmyadmin.</p>
  
  <p>After several minutes I type in "create procedure" dialog box and press GO, i got an error in my syntax.</p>
  
  <p>I've search on google about which one is wrong, but still not found. Please anyone give me some advice how to fix this problem.</p>
  
  <p>This is my 'MySQL' stored procedure query.</p>
  
  <pre><code>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)&gt;(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)&gt;'08:15:00' THEN 'Late' ELSE 'On Time' END)
                WHEN a.TypeID=6 OR a.TypeID=7 THEN
                      (CASE WHEN TIME(c.EventTime)&gt;(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)&gt;(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)&gt;(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)&gt;(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)&gt;'08:15:00' THEN 'Late' ELSE 'On Time' END)
                WHEN a.TypeID=6 OR a.TypeID=7 THEN
                      (CASE WHEN TIME(c.EventTime)&gt;(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)&gt;(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)&gt;(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$$
  </code></pre>
  
  <p>I got error like this</p>
  
  <p>processing your request:</p>
  
  <blockquote>
    <p>The following query has failed: "CREATE DEFINER=<code>root</code>@<code>%</code> PROCEDURE <code>USP_SelectAttendance</code>(IN <code>cek</code> BIT(1), IN <code>type</code> INT, IN <code>sdate</code> DATETIME, IN <code>search</code> VARCHAR(100)) NOT DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER  .......</p>
    
    <p>MySQL said: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '$$ BEGIN IF cek=1 THEN SELECT a.NIK AS NIK , a.EmployeeName AS ' at line 1</p>
  </blockquote>
  
  <p>In addition, the 'SELECT' query is work fine when i run it in SQL.</p>
  
  <p>The error was appear in stored procedure creation.</p>
  
  <p>PS: I'm sorry for bad english</p>