MySQL stored procedure syntax error after BEGIN - mysql

I am attempting to recreate a stored procedure (since I can't edit the body). I called SHOW CREATE PROCEDURE to use the same format as the original stored procedure but when I attempt to recreate it I get the following errors:
ERROR 1064 (42000): 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 '' at line 11
ERROR 1064 (42000): 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 'DECLARE organization_id BIGINT(20) UNSIGNED' at line 1
ERROR 1064 (42000): 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 'DECLARE lobby_pod_id BIGINT(20) UNSIGNED' at line 1
Here's the code:
CREATE DEFINER=`lms`#`10.0.0.%` PROCEDURE `create_organization`(
IN admin_username VARCHAR(255),
IN organization_name VARCHAR(100)
)
BEGIN
DECLARE admin_user_id BIGINT(20) UNSIGNED;
DECLARE organization_id BIGINT(20) UNSIGNED;
DECLARE lobby_pod_id BIGINT(20) UNSIGNED;
SELECT ID, account INTO admin_user_id, organization_id
FROM users
WHERE username = admin_username;
INSERT INTO pods (`title`, `description`, `owner`, `scene`)
VALUES (CONCAT(organization_name, " Village"),
CONCAT("General meeting space and hub for ", organization_name, " students and teachers."),
admin_user_id,
" Village"
);
END
I pasted into SQL Fiddle and got the same result, although pasting into MySQL Syntax Check gave me the thumbs-up. I'm sure it's a simple miss but it isn't that obvious to me.

You are missing the delimiter definition before and after the stored proc definition:
If you use the mysql client program to define a stored program containing semicolon characters, a problem arises. By default, mysql itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause mysql to pass the entire stored program definition to the server.
To redefine the mysql delimiter, use the delimiter command. [...] The delimiter is changed to // to enable the entire
definition to be passed to the server as a single statement, and then
restored to ; before invoking the procedure. This enables the ;
delimiter used in the procedure body to be passed through to the
server rather than being interpreted by mysql itself.
Since the stored proc definition and body was ok, syntax chack gave you the thumbs up, but the code would not run properly in your client.
Use the following skeleton for defining a stored procedure:
delimiter //
create procedure ...
...
end
//
delimiter ;

Related

Why I can't I fix MySQL error #1064? Syntax related to DECLARE CheckExists int; [duplicate]

I am attempting to recreate a stored procedure (since I can't edit the body). I called SHOW CREATE PROCEDURE to use the same format as the original stored procedure but when I attempt to recreate it I get the following errors:
ERROR 1064 (42000): 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 '' at line 11
ERROR 1064 (42000): 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 'DECLARE organization_id BIGINT(20) UNSIGNED' at line 1
ERROR 1064 (42000): 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 'DECLARE lobby_pod_id BIGINT(20) UNSIGNED' at line 1
Here's the code:
CREATE DEFINER=`lms`#`10.0.0.%` PROCEDURE `create_organization`(
IN admin_username VARCHAR(255),
IN organization_name VARCHAR(100)
)
BEGIN
DECLARE admin_user_id BIGINT(20) UNSIGNED;
DECLARE organization_id BIGINT(20) UNSIGNED;
DECLARE lobby_pod_id BIGINT(20) UNSIGNED;
SELECT ID, account INTO admin_user_id, organization_id
FROM users
WHERE username = admin_username;
INSERT INTO pods (`title`, `description`, `owner`, `scene`)
VALUES (CONCAT(organization_name, " Village"),
CONCAT("General meeting space and hub for ", organization_name, " students and teachers."),
admin_user_id,
" Village"
);
END
I pasted into SQL Fiddle and got the same result, although pasting into MySQL Syntax Check gave me the thumbs-up. I'm sure it's a simple miss but it isn't that obvious to me.
You are missing the delimiter definition before and after the stored proc definition:
If you use the mysql client program to define a stored program containing semicolon characters, a problem arises. By default, mysql itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause mysql to pass the entire stored program definition to the server.
To redefine the mysql delimiter, use the delimiter command. [...] The delimiter is changed to // to enable the entire
definition to be passed to the server as a single statement, and then
restored to ; before invoking the procedure. This enables the ;
delimiter used in the procedure body to be passed through to the
server rather than being interpreted by mysql itself.
Since the stored proc definition and body was ok, syntax chack gave you the thumbs up, but the code would not run properly in your client.
Use the following skeleton for defining a stored procedure:
delimiter //
create procedure ...
...
end
//
delimiter ;

Setting a parameter in stored procedure raises an error

I'm trying to create a stored_procedure using the following sql code:
INSERT INTO users (name,surname,email,phone,address_id)
VALUES (name,surname,email,phone,address_id);
SET #GeneratedUserID = LAST_INSERT_ID();
INSERT INTO user_login (username,password_salt,hash_code,users_user_id)
VALUES (username,password_salt,hash_code,#GeneratedUserID );
INSERT INTO user_roles (user_id,role_id)
VALUES (#GeneratedUserID ,1);
However, I'm getting an error:
MySQL said: #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 'SET #GeneratedUserID = LAST_INSERT_ID(); INSERT INTO user_login (username,pas' at line 4
You most likely haven't changed the delimiter, but can't tell for sure, because you didn't post the whole code (please always do this). Therefore MySQL thinks, that the procedure is finished after the first ;.
Your procedure should look like this:
delimiter $$
create procedure x(in p_param1 int, in p_param2 int)
begin
statement1;
statement2;
end$$
delimiter ;
Also note, that you should choose parameter names that are not the same as column names. Best practice is to use a prefix like p_. Same for variables. Use a prefix like v_ or something.

Sql Syntax Error, on CREATE PROCEDURE

Work with mysql on phpMyAdmin
SQL :
drop PROCEDURE if EXISTS mi;
CREATE PROCEDURE mi() //<-- line 3
BEGIN
INSERT INTO User ( `name` , `password` ) VALUES ('value1', 'value2');
SET out_param = LAST_INSERT_ID();
END
CALL mi();
the 'drop' is for multi testing
we have table User there are id primary key , name varchar, password varchar
Error :
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 3
thanks for any help !
By default, mysql itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause mysql to pass the entire stored program definition to the server. To redefine the mysql delimiter, use the delimiter command.
You can read about this in more detail create procedure.
Add "delimiter //"before create statement and "//" after end statement in order to execute your procedure. You can use any other delimiter as well.
In phpMyAdmin, you can set the delimiter on the SQL page, near the bottom, there is a line on the same area of the page as the Go button. Change the separator to anything ($$ is a common one) and then add this to the end of your script.

MySQL Syntax Error when Select Count into inout Variable

Here is the code:
CREATE PROCEDURE CountOrderByStatus(
IN orderStatus VARCHAR(25),
OUT total INT)
BEGIN
SELECT count(orderNumber)
INTO total
FROM orders
WHERE status = orderStatus;
END
When I try to execute this, i get the following error:
Error SQL (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 '' at line 8
I was googling for a while, but I can't find any information about this problem. Many sources does not refer such type of code as invalid. For example, fragment above found at this site.
Any ideas, why everybody is saying, that is normal, but MySQL produces such strange error?
P.S.: MySQL Server 5.6 at Windows 8.1.
Was forget to add DELIMITER. It must look:
DELIMITER $$
CREATE PROCEDURE CountOrderByStatus(
IN orderStatus VARCHAR(25),
OUT total INT)
BEGIN
SELECT count(orderNumber)
INTO total
FROM orders
WHERE status = orderStatus;
END$$
DELIMITER ;

Pretty printing not working for stored proc in MySQL

I am trying to write an simple stored procedure to just print out information from three different tables. The problem is that for some reason \G does not work from within a stored proc. I want the output to be readable so this is pretty important
This code works but doesnt display columns in an effective way
DELIMITER //
DROP PROCEDURE IF EXISTS `snapshot`//
CREATE PROCEDURE snapshot(IN employeeUsername varchar(255))
BEGIN
SELECT * FROM employee where username = employeeUsername;
END //
DELIMITER ;
This code throws an exception
DELIMITER //
DROP PROCEDURE IF EXISTS `snapshot`//
CREATE PROCEDURE snapshot(IN employeeUsername varchar(255))
BEGIN
SELECT * FROM employee where username = employeeUsername \G;
END //
DELIMITER ;
ERROR 1064 (42000): 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 '' at line 3
Any help would be greatly appreciated
\G is an command in MySQL Client and subsequently can not be called from a sql stored proc