The error message you encountered typically suggests that there is a syntax error near the beginning of your SQL statement. In your case, it appears that the procedure definition is not properly formatted, particularly with the proper termination of SQL statements and potentially issues with the delimiters used for procedure creation.
MySQL and MariaDB require precise syntax for creating stored procedures, including changing the delimiter so that the stored procedure's internal statements are treated correctly.
Here’s how you can define your stored procedure with the correct syntax and delimiter in MySQL or MariaDB:
-- Change the delimiter to avoid confusion with semicolons inside the procedure
DELIMITER //
CREATE PROCEDURE DodajGracza(
IN pcustomerName VARCHAR(100),
IN pcreditLimit INT
)
BEGIN
INSERT INTO customers (customerName, creditLimit)
VALUES (pcustomerName, pcreditLimit);
END //
-- Change the delimiter back to semicolon
DELIMITER ;
Explanation:
-
DELIMITER //: This changes the statement delimiter from ; to //. It lets you use ; inside your stored procedure without signaling the end of the entire procedure to the SQL interpreter.
-
CREATE PROCEDURE: This is the command to create a new stored procedure.
-
IN pcustomerName VARCHAR(100), IN pcredit>() INT: These are the input parameters to the procedure. Here, pcustomerName is expected to be a string with a maximum length of 100 characters, and pcreditLimit is an integer.
-
BEGIN...END: Encloses the statements that make up the body of the procedure. All the operations the procedure should perform are placed between these keywords.
-
INSERT INTO customers...: This SQL statement inside the BEGIN and END block is the core operation performed by this procedure - inserting a new row into the 'customers' table.
-
END //: Ends the procedure definition. Note the use of // which is the temporary delimiter we declared.
-
DELIMITER ;: Resets the statement delimiter back to the standard semicolon. This is important to restore normal operation after the procedure creation.
Make sure that the table customers exists in your database, and that it has columns named customerName and creditLimit. If any of these conditions don't match (like if the table or columns have different names, or if the table doesn't exist), you will face an error when executing this procedure. Be sure also that the database user has the necessary permissions to insert records into the customers table.