Error 1064 encountered while creating MySQL stored procedure
P粉041881924
P粉041881924 2023-09-12 15:41:52
0
1
645

I want to create a stored procedure in MySQL, but I encountered an error 1064:

错误代码:1064
您的SQL语法有误;请检查与您的MariaDB服务器版本相对应的手册,以获取正确的语法使用方法,位于第2行附近的 'ZID_TRANSAKSI VARCHAR(64),
   ZTGL1 DATE,
   ZTGL2 DATE 
) 
BEGIN 
  DECLARE ...' 

查询:DELIMITER;

错误代码:1064
您的SQL语法有误;请检查与您的MariaDB服务器版本相对应的手册,以获取正确的语法使用方法,位于第1行附近的 'DELIMITER'

This is the query for the stored procedure I want to create:

DELIMITER $$ CREATE PROCEDURE HITUNGMASUK 
(
   ZID_TRANSAKSI VARCHAR(64),
   ZTGL1 DATE,
   ZTGL2 DATE 
) 

BEGIN 
  DECLARE done INT DEFAULT FALSE;
   DECLARE PTAHUN VARCHAR(4);
   DECLARE PBULAN VARCHAR(2);
   DECLARE PHITUNG INTEGER DEFAULT 0; 
   DECLARE CEKKARTUSTOK INTEGER DEFAULT 0;
   DECLARE PID_BARANG VARCHAR(255) DEFAULT 'X';
   DECLARE PPANJANG INTEGER DEFAULT 0;
   DECLARE CEKKARTUSTOK CURSOR FOR
   SELECT B.id_barang, SUM(B.total_Panjang) AS PANJANG
      FROM transaksi_gudang A,
           line_item_barang B
      WHERE A.ID_Transaksi LIKE ZID_TRANSAKSI 
      AND   A.Tanggal BETWEEN ZTGL1 AND ZTGL2
      AND   B.ID_GR = A.ID_Transaksi
      GROUP BY B.id_barang;
    
   DECLARE
     CONTINUE HANDLER FOR NOT FOUND
   SET
    done = TRUE;


   PTAHUN = YEAR(ZTGL1);
   PBULAN = MONTH(ZTGL1);
  
   OPEN CEKKARTUSTOK;
   teams_loop:
   LOOP
      FETCH CEKKARTUSTOK INTO PID_BARANG,PPANJANG;
   
       IF PID_BARANG = 'X' THEN
          INSERT INTO kartustok(TAHUN,BULAN,ID_BARANG,KODE_GUDANG,MASUK) VALUES ();
       END IF;    
   
      IF done THEN LEAVE teams_loop;
      END IF;

   END LOOP;
  CLOSE CEKKARTUSTOK;


   
   SELECT 
   FROM kartustok C 
   WHERE C.TAHUN = PTAHUN
   AND   C.BULAN = PBULAN
   AND   C.ID_BARANG = P
   
   
END $$ DELIMITER;

Why does my query show Error code: 1064? How can I fix it?

I used ORACLE database before, and now I just started using MySQL. If there are any differences that I'm not aware of, please let me know.

P粉041881924
P粉041881924

reply all(1)
P粉883223328

DELIMITER Only required when you create a stored procedure within the command line client, to distinguish between regular delimiters and delimiters inside BEGIN END blocks.

In your example you define a separator $$ CREATE PROCEDURE HITUNGMASUK, which is probably not what you really want.

DELIMITER $$ needs to be executed as a single command and not connected to a stored procedure definition.

DELIMITER $$
CREATE PROCEDURE()
BEGIN
...
END
$$
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template