Nice, good that GetSimpleCustomers() works. Let us rebuild the whole explanation properly around that style and then add the other methods.
1. What is a stored procedure?
A stored procedure is a saved SQL program in the database.
- You define it once using
CREATE PROCEDURE. - After that, you call it with
CALL procedureName(...). - It lives inside the database (like a reusable script).
You use stored procedures to:
- Reuse complex queries
- Hide logic from application code
- Enforce consistent business rules
- Improve maintainability
2. The simple format that works in DbVisualizer
This is the format that worked for you:
CREATE PROCEDURE GetSimpleCustomers()
SELECT
customerName,
city,
state,
postalCode,
country
FROM customers
ORDER BY customerName;
Key points:
- No
BEGINandEND - No
DELIMITERchanges - Exactly one SQL statement after
CREATE PROCEDURE ...()
This style is valid in MySQL when the procedure body is just a single SQL statement.
2.1 Step by step in DbVisualizer using this format
- Open SQL Commander in DbVisualizer
- Select the correct database
- Paste your procedure:
- Execute the SQL
- Confirm it exists
Tools → SQL Commander → New SQL Commander
At the top, set the database to classicmodels or whichever you are using.
CREATE PROCEDURE GetSimpleCustomers()
SELECT
customerName,
city,
state,
postalCode,
country
FROM customers
ORDER BY customerName;
Use normal execute (Ctrl + Enter).
In the database tree, expand your database → Procedures.
You should see GetSimpleCustomers.
2.2 How to call the stored procedure
Once created, call it like this:
CALL GetSimpleCustomers();
or, with schema:
CALL classicmodels.GetSimpleCustomers();
Run that as a normal statement in SQL Commander.
You will see a result grid with your customers.
3. Adding parameters using the simple style
You can still use parameters with the simple format, as long as the body is a single statement.
Example: customers by country
CREATE PROCEDURE GetCustomersByCountry(IN p_country VARCHAR(50))
SELECT
customerName,
city,
state,
postalCode,
country
FROM customers
WHERE country = p_country
ORDER BY customerName;
Call it like this:
CALL GetCustomersByCountry('USA');
Explanation:
IN p_country VARCHAR(50)is an input parameter.- You can use the parameter in the query just like a column or variable.
Another example: customers over a credit limit
CREATE PROCEDURE GetCustomersOverLimit(IN p_limit DECIMAL(10,2))
SELECT
customerName,
creditLimit,
city,
country
FROM customers
WHERE creditLimit > p_limit
ORDER BY creditLimit DESC;
Call:
CALL GetCustomersOverLimit(50000.00);
4. When do you need BEGIN and END?
You must move to the multi-statement style when you need:
- More than one SQL statement in the procedure
- Local variables
- Flow control
- Output parameters that require setting values from logic
example: SET variable then SELECT
DECLARE v INT;
IF, CASE, WHILE, LOOP, etc.
Example in standard MySQL syntax:
DELIMITER $$
CREATE PROCEDURE HelloUser()
BEGIN
DECLARE v CHAR(10) DEFAULT 'Hello';
SELECT CONCAT(v, ', ', CURRENT_USER(), '!');
END$$
DELIMITER ;
In normal MySQL clients (CLI, Workbench), this is fine.
In DbVisualizer, you need to be careful.
5. Multi-statement procedures in DbVisualizer
DbVisualizer gets confused by ; inside a procedure, because it usually treats it as the end of the statement.
There are two main approaches.
5.1 Using DELIMITER plus “Execute Buffer as Script”
Write your multi-statement procedure like this:
DELIMITER $$
CREATE PROCEDURE HelloUser()
BEGIN
DECLARE v CHAR(10) DEFAULT 'Hello';
SELECT CONCAT(v, ', ', CURRENT_USER(), '!');
END$$
DELIMITER ;
Important in DbVisualizer:
- Put all of that in one SQL Commander buffer
- Do not run line by line
- Use “Execute Buffer as Script” (Ctrl + Shift + Enter)
That tells DbVisualizer to send the whole block correctly, including the delimiter changes.
Call it afterwards with:
CALL HelloUser();
5.2 Using DbVisualizer’s procedure editor
Some DbVisualizer versions have a procedure editor, which hides the delimiter issues.
Typical flow:
- In the database tree, right click the schema
- Paste this body:
- Click Execute in that dialog
Choose Create → Procedure or similar
CREATE PROCEDURE HelloUser()
BEGIN
DECLARE v CHAR(10) DEFAULT 'Hello';
SELECT CONCAT(v, ', ', CURRENT_USER(), '!');
END
DbVisualizer takes care of sending it correctly.
6. IN, OUT, and INOUT parameters
So far you used IN parameters. There are three types:
INOUTINOUT
Value is passed into the procedure. You cannot change it for the caller.
Procedure sets the value and returns it out.
Caller passes a value in, procedure can modify it and pass back.
Simple OUT example:
DELIMITER $$
CREATE PROCEDURE CountCustomersByCountry(
IN p_country VARCHAR(50),
OUT p_count INT
)
BEGIN
SELECT COUNT(*)
INTO p_count
FROM customers
WHERE country = p_country;
END$$
DELIMITER ;
Calling from SQL:
SET @c := 0;
CALL CountCustomersByCountry('USA', @c);
SELECT @c AS total_customers_in_usa;
In DbVisualizer, you run this whole block as “Execute Buffer as Script”.
Use OUT and INOUT only when you really need result variables.
Most reporting-style procedures can just return a result set using SELECT.
7. Managing stored procedures
7.1 See the procedure definition
Use:
SHOW CREATE PROCEDURE GetSimpleCustomers;
This shows how MySQL actually stored it.
7.2 List procedures in a schema
SHOW PROCEDURE STATUS
WHERE Db = 'classicmodels';
Or just look in the DbVisualizer database tree under Procedures.
7.3 Drop a procedure
If you want to recreate or remove it:
DROP PROCEDURE IF EXISTS GetSimpleCustomers;
Then create it again with new logic.
8. Practical summary for you
For your current learning and classicmodels work:
- Prefer the simple pattern in DbVisualizer when possible:
- Use
INparameters to make the procedure dynamic. - Call with
CALL ProcName(...); - Move to
BEGINandENDonly when you need variables or multiple statements. - When using
BEGINandEND, either: - use
DELIMITERplus “Execute Buffer as Script”, or - use the DbVisualizer procedure editor.
CREATE PROCEDURE ProcName(...)
SELECT ...;
If you like, next we can design two or three useful procedures specifically for classicmodels that support your finance and analytics learning, for example:
- top customers by sales
- late payments or high credit risk customers
- sales summary by country and year.