Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Procedure MySQL

Tags:

mysql

Im trying to convert a procedure from using sql plus to mysql but am getting a syntax error on the third line where it says (W_IN IN NUMBER) and it has IN highlighted as the syntax error.

SQL Plus:

CREATE OR REPLACE PROCEDURE PRC_CUS_BALANCE_UPDATE (W_IN IN NUMBER) AS
W_CUS NUMBER := 0;
W_TOT NUMBER := 0;
BEGIN
-- GET THE CUS_CODE
SELECT CUS_CODE INTO W_CUS 
FROM INVOICE
WHERE INVOICE.INV_NUMBER = W_IN;

-- UPDATES CUSTOMER IF W_CUS > 0
IF W_CUS > 0 THEN
    UPDATE CUSTOMER
    SET CUS_BALANCE = CUS_BALANCE + 
                (SELECT INV_TOTAL FROM INVOICE WHERE INV_NUMBER = W_IN)
    WHERE CUS_CODE = W_CUS;
END IF;
END;

mySQL:

-- Trigger DDL Statements
DELIMITER $$

CREATE PROCEDURE prc_cus_balance_update (W_IN IN NUMBER) 
AS
W_CUS NUMBER = 0;
W_TOT NUMBER = 0;
BEGIN
-- GET CUS_CODE
SELECT CUS_CODE INTO W_CUS 
FROM INVOICE
WHERE INVOICE.INV_NUMBER = W_IN;

-- UPDATES CUSTOMER IF W_CUS > 0
IF W_CUS > 0 THEN
    UPDATE CUSTOMER
    SET CUS_BALANCE = CUS_BALANCE + 
                (SELECT INV_TOTAL FROM INVOICE WHERE INV_NUMBER = W_IN)
    WHERE CUS_CODE = W_CUS;
END IF;
END $$

DELIMITER ;

Any help is greatly appreciated!

like image 308
Noah Jones Avatar asked Oct 05 '12 22:10

Noah Jones


People also ask

How do you create a procedure in a database?

To create a procedure in Object ExplorerIn Object Explorer, connect to an instance of Database Engine and then expand that instance. Expand Databases, expand the AdventureWorks2019 database, and then expand Programmability. Right-click Stored Procedures, and then click New Stored Procedure.

What does CREATE PROCEDURE do in SQL?

The CREATE PROCEDURE SQL command is used to create a procedure, followed by a SP name and its parameters. The BEGIN and END area is used to define the query for the operation. This is where you will write a select, update, insert, or delete queries.

What is procedural MySQL?

A procedure is a subroutine (like a subprogram) in a regular scripting language, stored in a database. In the case of MySQL, procedures are written in MySQL and stored in the MySQL database/server. A MySQL procedure has a name, a parameter list, and SQL statement(s).


2 Answers

This compiles in MySQL 5.5.23:

-- Trigger DDL Statements
DELIMITER $$

DROP PROCEDURE IF EXISTS prc_cus_balance_update;

CREATE PROCEDURE prc_cus_balance_update (IN W_IN INT UNSIGNED) 
BEGIN
DECLARE W_CUS INT UNSIGNED DEFAULT 0;
DECLARE W_TOT DOUBLE DEFAULT 0; -- NOT USED?
-- GET CUS_CODE
SELECT CUS_CODE INTO W_CUS 
FROM INVOICE
WHERE INVOICE.INV_NUMBER = W_IN;

-- UPDATES CUSTOMER IF W_CUS > 0
IF W_CUS > 0 THEN
    UPDATE CUSTOMER
    SET CUS_BALANCE = CUS_BALANCE + 
                (SELECT INV_TOTAL FROM INVOICE WHERE INV_NUMBER = W_IN)
    WHERE CUS_CODE = W_CUS;
END IF;
END $$

DELIMITER ;

Of course, in this case, a stored procedure is not needed, as the following query will perform the same function much faster (and easier to understand):

UPDATE 
    CUSTOMER c
INNER JOIN
    INVOICE i ON i.CUS_CODE = c.CUS_CODE
SET 
    c.CUS_BALANCE = c.CUS_BALANCE + i.INV_TOTAL 
WHERE 
    i.INV_NUMBER = W_IN
like image 164
Ross Smith II Avatar answered Oct 14 '22 15:10

Ross Smith II


The doc seems to say

 [ IN | OUT | INOUT ] param_name type
like image 45
Alain Collins Avatar answered Oct 14 '22 14:10

Alain Collins