2

I've already confused so that, I am asking here, about how to create a stored procedure in mysql with below code :

DELIMITER $$
CREATE PROCEDURE hitung_nilai_stok(sisa INT)

BEGIN
    DECLARE done INT DEFAULT FALSE;
    DECLARE jumlah DECIMAL;
    DECLARE harga INT;
    DECLARE nilai_stok INT DEFAULT 0;
    DECLARE selisih DECIMAL DEFAULT 0.000;
    DECLARE data_stok CURSOR FOR SELECT
        Stok.masuk,
        Stok.harga_masuk

        FROM stoks Stok
        WHERE Stok.barang_id = 1
        AND Stok.tanggal <= '2013-11-19';

    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

    TRUNCATE TABLE transaksi;

    SET selisih = sisa;
    OPEN data_stok;

    loop_data: LOOP
        FETCH data_stok INTO jumlah, harga;
        SET selisih = selisih - jumlah;

        IF done THEN
            LEAVE loop_data;
        END IF;

        IF (selisih = 0.000) THEN
            INSERT INTO transaksi VALUES (jumlah,harga);
        ELSE IF (selisih > 0.000) THEN
            SET selisih = selisih;
            INSERT INTO transaksi VALUES (jumlah,harga);
        ELSE IF (selisih < 0.000) THEN
            INSERT INTO transaksi VALUES (selisih,harga);
            SET done = TRUE;
        END IF;
    END LOOP loop_data;

    CLOSE data_stok;

    SELECT SUM(jumlah*harga) FROM transaksi;

END$$

DELIMITER ;

The error I got is :

#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 'LOOP loop_data; CLOSE data_stok; SELECT SUM(jumlah*harga) INTO nilai_stok FR' at line 49

Well, when I removed these lines :

ELSE IF (selisih > 0.000) THEN
    SET selisih = selisih;
    INSERT INTO transaksi VALUES (jumlah,harga);
ELSE IF (selisih < 0.000) THEN
    INSERT INTO transaksi VALUES (selisih,harga);
    SET done = TRUE;

It executed successfully. So, what is it that made the error occured? Can somebody gimme some clues?

0

1 Answer 1

1

Your ELSE IF is wrong, it should be ELSEIF together

Try this

IF (selisih = 0.000) THEN
            INSERT INTO transaksi VALUES (jumlah,harga);
        ELSEIF (selisih > 0.000) THEN
            SET selisih = selisih;
            INSERT INTO transaksi VALUES (jumlah,harga);
        ELSEIF (selisih < 0.000) THEN
            INSERT INTO transaksi VALUES (selisih,harga);
            SET done = TRUE;
 END IF;
Sign up to request clarification or add additional context in comments.

3 Comments

Oh my god.. I really believe it would be a 'simple' mistake. Thank you for pointing me the mistake :D Executed nicely now.
Usually it is a simple mistake but its also easy to miss :). Do you make use of MySQL Workbench?
Nope. I just using phpmyadmin :D I thought i more than enough for me (at least for now)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.