Setting a parameter in stored procedure raises an error - mysql

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.

Related

MySQL | MariaDB Creates a #1064 syntax error on import [duplicate]

I am having some trouble with creating a Stored Procedure for a MySQL database.
Here is a Select statement that works:
use canningi_db_person_cdtest;
SELECT *
FROM pet
WHERE name = 'Puffball';
Here is my Stored Procedure that does not work:
use canningi_db_person_cdtest;
CREATE PROCEDURE GetAllPets()
BEGIN
SELECT *
FROM pet
WHERE name = 'Puffball';
END
I am getting the following error:
#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 5
How can I get this working?
EDIT
How do I call this Stored Procedure? I have tried this with no result:
use canningi_db_person_cdtest;
CALL GetAllPets();
This is the error:
#1312 - PROCEDURE canningi_db_person_cdtest.GetAllPets can't return a result set in the given context
Add a delimiter to end your procedure. Example:
use canningi_db_person_cdtest;
DELIMITER //
CREATE PROCEDURE GetAllPets()
BEGIN
SELECT *
FROM pet
WHERE name = 'Puffball';
END//
If you need more information, please refer to http://dev.mysql.com/doc/refman/5.1/en/stored-programs-defining.html
In this particular case, since you have only one statement in your procedure, you don't need to change DELIMITER nor use BEGIN...END block.
You procedure might look like this
CREATE PROCEDURE GetAllPets()
SELECT *
FROM pet
WHERE name = 'Puffball';
And use it like this
CALL GetAllPets();
Here is SQLFiddle demo

MySQL: Trying to Create a Trigger Where Another Table is Updated

I'm trying to create a trigger whereby an insertion on one table updates another. This is my SQL Query:
CREATE TRIGGER makePayment AFTER INSERT ON Payments FOR EACH ROW
BEGIN
UPDATE Invoice
SET InvoiceClientPaid = SUM(InvoiceClientPaid + NEW.PaymentAmt)
WHERE InvoiceID = NEW.PaymentInvoiceID;
END;
No matter what I do I get the following error:
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 '' at line 6
I don't think it's related to the SUM, because trying a basic = 1 on the SET command gives me the exact error. There is no '' at line 6 which is very confusing?
If you're entering this query directly into MySQL you will need to change the delimiter prior to the query using (e.g.) DELIMITER //, otherwise it thinks the query ends at the ; at the end of your UPDATE statement. MySQL then sees an END with nothing before it and complains about the nothing (''). So try this:
DELIMITER //
CREATE TRIGGER makePayment AFTER INSERT ON Payments FOR EACH ROW
BEGIN
UPDATE Invoice
SET InvoiceClientPaid = SUM(InvoiceClientPaid + NEW.PaymentAmt)
WHERE InvoiceID = NEW.PaymentInvoiceID;
END; //
DELIMITER ;

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 server stored procedure syntax error

I am having some trouble with creating a Stored Procedure for a MySQL database.
Here is a Select statement that works:
use canningi_db_person_cdtest;
SELECT *
FROM pet
WHERE name = 'Puffball';
Here is my Stored Procedure that does not work:
use canningi_db_person_cdtest;
CREATE PROCEDURE GetAllPets()
BEGIN
SELECT *
FROM pet
WHERE name = 'Puffball';
END
I am getting the following error:
#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 5
How can I get this working?
EDIT
How do I call this Stored Procedure? I have tried this with no result:
use canningi_db_person_cdtest;
CALL GetAllPets();
This is the error:
#1312 - PROCEDURE canningi_db_person_cdtest.GetAllPets can't return a result set in the given context
Add a delimiter to end your procedure. Example:
use canningi_db_person_cdtest;
DELIMITER //
CREATE PROCEDURE GetAllPets()
BEGIN
SELECT *
FROM pet
WHERE name = 'Puffball';
END//
If you need more information, please refer to http://dev.mysql.com/doc/refman/5.1/en/stored-programs-defining.html
In this particular case, since you have only one statement in your procedure, you don't need to change DELIMITER nor use BEGIN...END block.
You procedure might look like this
CREATE PROCEDURE GetAllPets()
SELECT *
FROM pet
WHERE name = 'Puffball';
And use it like this
CALL GetAllPets();
Here is SQLFiddle demo

MySql on update trigger. Error of DELIMITER

I try to create trigger in MySql but get the following error:
#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 'DELIMITER' at line 1
DELIMITER $$
CREATE TRIGGER library_update
AFTER UPDATE ON wq6vt_vehiclemanager_vehicles
FOR EACH ROW
BEGIN
INSERT IGNORE INTO wq6vt_vehiclemanager_library (maker, model) VALUES(NEW.maker, NEW.vmodel);
INSERT INTO wq6vt_vehiclemanager_library_data (co2_class)
SELECT co2_class FROM wq6vt_vehiclemanager_vehicles
WHERE maker = NEW.maker AND vmodel = NEW.vmodel;
END $$
DELIMITER;
The first query in the trigger do not lead to errors, but second one does. There is some problem with SELECT inside of INSERT ...I think so
there should be a space between the keyword and the symbol,
DELIMITER ;
-- ^ space in between here