Sql Syntax Error, on CREATE PROCEDURE - mysql

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.

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.

MySQL Insert after trigger clarification

I have this code here:
CREATE TRIGGER testTrigger
AFTER INSERT ON users
BEGIN
DECLARE #uid VARCHAR(60)
SET #uid = (SELECT userid FROM inserted)
INSERT INTO user_locations (id,uid,lat,lng) VALUES (0,#uid,5.0,5.0)
END;
The idea is to insert generated user id into other table alongside some other data as soon as it hits the first 'users' table but phpMyAdmin gives this error code:
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
BEGIN
DECLARE #uid VARCHAR(60)
SET #uid = (SELECT userid FROM inserted)
at line 3
Can someone clarify why this trigger is bad?
I see four problems:
You have to use DELIMITERs so that your able to finish the commands with a semicolon as usual.
FOR EACH ROW is missing.
Use new.uid to access the recently inserted uid.
I'd also suggest using procedure variables instead of session-specific user-defined #variables, the latter ones being loosely typed and not declared as you've done.
But you don't even have to declare a variable. If you don't use phpMyAdmin:
DELIMITER //
CREATE TRIGGER testTrigger
AFTER INSERT ON users FOR EACH ROW
BEGIN
INSERT INTO user_locations (id,uid,lat,lng) VALUES (0,new.uid,5.0,5.0);
END//
DELIMITER ;
Check this answer about delimiter and the MySQL 5.7 docs on triggers and this answer about variables.
Edit, I overread you're using phpMyAdmin:
I don't use phpMyAdmin. But you can (stolen from here)
In phpMyAdmin, select the database that you want to work with.
Go to the SQL tab at the top of the page.
In the "Run SQL query/queries on database" form, change the Delimiter to $$. (Located in a small box at the bottom of the form)
Enter your SQL trigger into the main dialog box on the form. The correct syntax is as follows:
CREATE TRIGGER testTrigger
AFTER INSERT ON users FOR EACH ROW
BEGIN
INSERT INTO user_locations (id,uid,lat,lng) VALUES (0,new.uid,5.0,5.0);
END;$$
Hit "GO" with Super privilege.

MySQL stored procedure syntax error after BEGIN

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 ;

MySQL server version for the right syntax to use near '' at line

I found similar problems but they always were in php.
code
CREATE TRIGGER PL_gl
BEFORE INSERT ON Zaznam_o_vstrel_br
for each row
BEGIN
IF (NEW.Autor_branky IS NOT NULL) then
UPDATE hrac
SET Pocet_golu = Pocet_golu + 1
WHERE ID_num = NEW.Autor_branky; /*ERROR HERE*/
END IF;
END;
I made so much modifications that I already don't know how is it correct.
hrac contains:
create table hrac (
ID_num INT AUTO_INCREMENT Primary key,
Jmeno VARCHAR (50) NOT NULL,
Jmeno_tymu VARCHAR (50) NOT NULL,
Datum_narozeni DATE,
Domovsky_klub VARCHAR (50),
Cislo_dresu INT,
Pocet_golu INT);
Error says 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 9. That doesn't tells me anything so I tried at least 20 combinations. And in SQL it worked without any problem.
Assuming the SQL is correct, here is what I believe is going on. In your begin... end statement, you have statements that end in semicolons, and it hits the one on line 9, and it thinks you've finished defining the trigger. The trigger at that point isn't defined with correct syntax, so it fails. Try defining a new delimiter this before your first statement:
DELIMITER //
Then, instead of putting a semicolon after your END statement, use the new delimiter:
END //
That will enable you to use semicolons in your trigger without a syntax error.