Can't create MySQL procedure to update table - mysql

I am trying to make a procedure to update an existing user. It receives the name of the user and then increments his points column. I have it like this:
CREATE PROCEDURE addPoints (IN nomeus varchar(20))
BEGIN
UPDATE User
SET
points=points+1
WHERE (nome=nomeus) ;
END;
However, I get this 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 8
How can I fix it?

When in doubt just go to dev.mysql they have some great documentation there.
http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html
Looks like you found the answer to your own question. However, if you change any of the code in the procedure, don't forget to drop the procedure before trying to create it again.
Drop Procedure if exists addPoints;
http://dev.mysql.com/doc/refman/5.0/en/drop-procedure.html

Yeah, I had to use delimiters:
delimiter //
CREATE PROCEDURE addPoints(IN nomeuser varchar(256))
BEGIN
UPDATE User
SET
points = points + 1
WHERE nome = nomeuser;
END;//
delimiter ;

Related

How to create stored procedures in SQL using cmd?

This is what i tried. I have table called crop, and want to create stored procedure of selecting all in that table.
Here is the error i got:
MariaDB [sample]> CREATE PROCEDURE AllFarmers
-> AS
-> SELECT * FROM crop
-> GO;
ERROR 1064 (42000): 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 'AS SELECT * FROM crop GO' at line 2
Thank you for your help in advance
I saw that the syntax is not correct on your procedure. You should replace AS by BEGIN and GO by END.
I put an example here
delimiter //
CREATE PROCEDURE AllFarmers ()
BEGIN
SELECT * FROM Test;
END;
//
call allfarmers()
Try it and tell me if there is any problem
Might also be worth mentioning you can a drop to the start in case the stored procedure already exists:
DROP PROCEDURE IF EXISTS `AllFarmers`

Unexplainable MySQL Error #1064 for Empty String On creating After Update Trigger?

I was running MySQL with Server Version: 10.1.34-MariaDB (Distributed within XAmpp 7.2.7-0-VC15-installer) on Windows 8.1 machine, using PhpMyadmin on Google Chrome to access Mysql database and i got this 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 4
for this create trigger syntax :
DROP TRIGGER IF EXISTS Tsopd_main_AfterUpdate;
CREATE TRIGGER Tsopd_main_AfterUpdate AFTER UPDATE
ON sopd_main FOR EACH ROW
BEGIN
SET #jns = 1;
END
When i changed the code like this :
DROP TRIGGER IF EXISTS Tsopd_main_AfterUpdate;
CREATE TRIGGER Tsopd_main_AfterUpdate AFTER UPDATE
ON sopd_main FOR EACH ROW
BEGIN
END
or like this:
DROP TRIGGER IF EXISTS Tsopd_main_AfterUpdate;
CREATE TRIGGER Tsopd_main_AfterUpdate AFTER UPDATE
ON sopd_main FOR EACH ROW
BEGIN
-- SET #jns = 1;
END
it worked.
Can anybody Help me to show me what is wrong?
Thank you.
You need to use the DELIMITER directive to change the query delimiter, so you can use ; inside the trigger definition.
DELIMITER $$
DROP TRIGGER IF EXISTS Tsopd_main_AfterUpdate$$
CREATE TRIGGER Tsopd_main_AfterUpdate AFTER UPDATE
ON sopd_main FOR EACH ROW
BEGIN
SET #jns = 1;
END$$
DELIMITER ;

#1064 Error SQL - Happens on all my procedures/functions

I know this question has been asked a bunch in different forms, but none of the ones I looked at (quite a bit) seemed to help me out in my specific case. I wrote a few functions and procedures and I always get the same error at the same spot. Here is my code:
DELIMITER |
DROP PROCEDURE IF EXISTS STUDENTS_BY_STATUS;
CREATE PROCEDURE STUDENTS_BY_STATUS (sts VARCHAR(10))
BEGIN
SELECT BannerId, Name FROM STUDENT WHERE Status = sts;
END |
DELIMITER;
This happens on all my procedures functions, this 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 'CREATE PROCEDURE STUDENTS_BY_STATUS (sts VARCHAR(10))
BEGIN
SELECT BannerId,' at line 2
On my other one its this:
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 'CREATE FUNCTION GoodGrade(letGrade VARCHAR(2)) RETURNS int
BEGIN
DECLARE v' at line 2
This happens whether or not I use | or // as the DELIMITER...can someone tell me what I'm doing wrong here? Thanks!
You've changed the delimiter, written a statement and then not delimited it according to your new definition
Change the line
DROP PROCEDURE IF EXISTS STUDENTS_BY_STATUS;
to
DROP PROCEDURE IF EXISTS STUDENTS_BY_STATUS |
Update
This wasn't obvious from the initial error message, but you have another error when you reset the Delimiter after the procedure definition. You need a space before the ';' on that one.
so change
DELIMITER;
to
DELIMITER ;
NB
The other use of ';' within your procedure definition is correct, because you want everything within the create statement to to be processed together.

Syntax error in mysql new version

I have a problem with this, and I keep getting this error
MySQL said: Documentation
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.
Do you have any ideas for why I keep getting this error?
DROP PROCEDURE IF EXISTS `prn_insert`;
CREATE PROCEDURE `prn_insert`(id int, name text, description text)
BEGIN
insert into test
select id,name,description;
END
The semicolon is ending the CREATE PROCEDURE statement. To get the entire statement, use a delimiter other than a semicolon. We frequently use $$ (two dollar signs) as a delimiter, but you can use any character sequence that doesn't appear within the statement(s) you want to execute.
For example:
DELIMITER $$
DROP PROCEDURE myproc $$
CREATE PROCEDURE myproc(arg INT)
BEGIN
DECLARE i INT DEFAULT 0;
SET i = 1;
END$$
DELIMITER ;
Once the new delimiter is set, it stays in effect until it's changed to something else. So. we usually want to set it back to semicolon immediately after the `CREATE PROCEDURE' statement.

MySQL CREATE TRIGGER and DELIMITER ISSUES

I am trying to make a database that does some inventory checking, and I am working on some triggers to update everything as required. In this case, that I want to do is to take the current availability (disponibilidad) of an ingredient from the ingredients table, add to it how much is taken or added, and then save that value into the table stock_disponibilidad, as you can see from the trigger code I am trying to use.
DELIMITER $$
CREATE TRIGGER `update_disponibilidad_variacion` BEFORE INSERT ON `stock_disponibilidad`
FOR EACH ROW BEGIN
DECLARE old_disp DOUBLE DEFAULT 0;
SELECT disponibilidad INTO old_disp FROM ingredientes WHERE id = NEW.ingredientes_id;
NEW.disponibilidad = old_disp + NEW.variacion;
END$$
DELIMITER ;
However, whenever I execute the query to create the trigger, I 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 '.disponibilidad = old_disp + NEW.variacion; END' at line 5
I've done everything I read about this issue, still without result. What am I doing wrong?