MySQL Stored Procedure CREATE Error - mysql

I have about 2 years of mySQL under my belt but am diving into stored procedures for the first time to create an internal analytic tool for my site. I am usually good at tracking down SQL errors but this one is eluding me.
The error I get is 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 '' at line 3
This is the code I execute to receive this error:
CREATE PROCEDURE STORE_ANALYTICS (IN chain VARCHAR(300))
BEGIN
UPDATE _analytics_clicks
SET chainCount = (chainCount + 1)
WHERE visitChain = chain;
IF SELECT ROW_COUNT() = 0 THEN
INSERT INTO _analytics_clicks (visitChain, chainCount)
VALUES (chain, 1);
END IF;
END|
It is worth noting that before I execute this, I executed
DELIMITER |
The structure of the table I am trying to alter is this:
chainID int(11) auto_increment
visitChain varchar(300)
chainCount int(11)
When I execute line 3 by itself, replacing visitChain=chain with visitChain='0' (0 is a test chain i enetered), the command runs fine and chainCount is incremented.
Any ideas on why I am getting this error/the stored procedure is not being created?
Thanks,
Matt
EDIT :
Included delimiter in SQL command:
DELIMITER |
CREATE PROCEDURE STORE_ANALYTICS (IN chain VARCHAR(300))
BEGIN
UPDATE _analytics_clicks SET chainCount = (chainCount+1) WHERE visitChain=chain;
IF ROW_COUNT() = 0 THEN
INSERT INTO _analytics_clicks (visitChain, chainCount) VALUES (chain, 1);
END IF;
END|
DELIMITER ;
Gave me this error:
Fatal error: Maximum execution time of 300 seconds exceeded in C:\wamp\apps\phpmyadmin3.2.0.1\libraries\import\sql.php on line 119

You can do
IF ROW_COUNT() = 0 THEN
rather than IF SELECT ROW_COUNT() = 0 THEN. No need for the SELECT.

I suspect that your delimiter is not being set properly.
I usually include the delimiter set/unset statements with my SQL source, that way "it just runs" - it's good practice that every SQL file can run stand-alone eg:
delimiter ~
create procedure ...
end;~
delimiter ;
Try changing your delimiter to something less deadly than a pipe char - try my tilda ~ char, which never gives me any trouble. I've seen $$ used often as well.

Related

#1064 - You have an error in your SQL syntax; , SAMP MYSQL Error

I get this error on my PHPMyAdmin
#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 3
[EDIT]
Sorry, I didn't put the full thing before, i've already had an BEGIN and an END, Look.
CREATE TRIGGER `accounts_insert` BEFORE INSERT ON `accounts`
FOR EACH ROW BEGIN
SET new.RegiDate = now();
SET new.UpdateDate = now();
END;
This is the line ^^
If your trigger is only one instruction, you don't need the begin keyword:
create trigger `accounts_insert` before insert on `accounts`
for each row
set new.RegiDate = now();
If your trigger has multiple instructions, then you need to:
Change the default delimiter
Enclose the trigger instructions in a begin...end block
Restore the default delimiter
Example:
delimiter $$
create trigger `accounts_insert_2` before insert on `accounts`
for each row
begin
set new.RegiDate = now();
set #newRows = coalesce(#newRows, 0) + 1; -- Just a dummy example
end; &&
delimiter ;
Why the delimiter $$ and delimiter ; are important?
When you write a query, MySQL assumes that it ends where it finds the first standard "instruction terminator" (;). Since your trigger has more than one instruction, and every instruction must end with ;, then MySQL assumes that the trigger definition is ended, and (of course) fails to execute it. So, what can be done? Simply redefine temporally the standard instruction terminator:
delimiter $$
Now, each instruction must end with $$ to be executed. Define your trigger, and, when you want to end the definition, use your new temporary instruction terminator $$, and then redefine the terminator to the normal ;:
delimiter ;
This must be done every time you define a multi insstruction trigger or a stored procedure or function.

error in writing procedure in Phpmyadmin

I'm trying to write a stored procedure in Mysql phpmyadmin, the procedure is
DELIMITER $$
DROP FUNCTION IF EXISTS `shopping_portal`.`f_authenticate_admin`$$
CREATE PROCEDURE =`root`#`localhost` FUNCTION `f_authenticate_admin`(l_username VARCHAR(50),l_password VARCHAR(50)) RETURNS int(11)
BEGIN
DECLARE exist INT DEFAULT 0;
SELECT count(*) INTO exist FROM admin WHERE username=l_username and password=MD5(l_password);
RETURN exist;
END$$
DELIMITER ;
but it is throwing the 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 '=`root`#`localhost` FUNCTION `f_authenticate_admin`(l_username VARCHAR(50),l_pas' at line 1
Help me in writing this procedure. Thanks in advance.
Well, multiple issues:
You're mixing procedures and functions, those are two different stories. And you're probably looking for DEFINER with your = root#localhost. And you want to use single quotes instead of backticks (I'm not sure though, if that's really an issue). Anyway, let me rewrite it for you...
DROP PROCEDURE IF EXISTS `shopping_portal`.`f_authenticate_admin`;
DELIMITER $$
CREATE DEFINER = 'root'#'localhost' PROCEDURE `f_authenticate_admin`(IN l_username VARCHAR(50), IN l_password VARCHAR(50), OUT result tinyint)
BEGIN
SELECT EXISTS(SELECT 1 FROM admin WHERE username=l_username and password=MD5(l_password)) INTO result;
END$$
DELIMITER ;
You then call it like this:
CALL f_authenticate_admin('test_username', 'a_password', #a_variable);
Then you have your result in #a_variable.
SELECT #a_variable;
Result is either 1 or 0.

Issues creating daily event

Trying to create a daily event in mysql:
CREATE EVENT ResetStatus
ON SCHEDULE
EVERY 1 DAY
DO
BEGIN
IF (DATE('2013-04-05') = CURDATE()) THEN
UPDATE mytable
SET resetstatus = 1
WHERE id = (SELECT pid FROM usertable WHERE priority = 'A');
END IF;
END;
Get an error:
Lookup Error - MySQL Database Error: 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 12
If i take the update statement and place in TOAD and run, it runs with no errors.
As documented under Defining Stored Programs:
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.
You need to set your client to use a statement delimiter other than ;, as it currently thinks that the first semicolon that it encounters (at the end of the UPDATE statement) is terminating the CREATE EVENT statement.
In the MySQL command line client, one can use the DELIMITER command:
DELIMITER ;; -- or anything else you like
CREATE EVENT ResetStatus
ON SCHEDULE
EVERY 1 DAY
DO
BEGIN
IF (DATE('2013-04-05') = CURDATE()) THEN
UPDATE mytable
SET resetstatus = 1
WHERE id = (SELECT pid FROM usertable WHERE priority = 'A');
END IF;
END
;;
DELIMITER ; -- return to normal

mysql error, stored procedure creation

I am new to mysql and I cant see why I have an error when I create my stored procedure.
DELIMITER |
CREATE PROCEDURE lastscan(IN task_id_var INT)
BEGIN
SELECT COUNT(*) FROM debugger WHERE task_id=task_id_var INTO #total|
SET #total=#total+1|
INSERT INTO debugger SET scan_num=#total, task_id=task_id_var|
END|
DELIMITER;
I get :
#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 3
I also dont get, why do I need to use that delimiter syntax.. ? DELIMITER | and then again DELIMITER;...what its function
DELIMITER |
CREATE PROCEDURE lastscan(IN task_id_var INT, IN file_name_var VARCHAR(110))
BEGIN
SELECT COUNT(*) FROM debugger WHERE task_id=task_id_var INTO #total;
SET #total=#total+1;
INSERT INTO debugger SET scan_num=#total, task_id=task_id_var, file_name=file_name_var;
END|
DELIMITER;
This works for me. no need to put | delimiter in sored procedure. I think it is meant to be for the stored procedure and not for what is inside the body
You can't simply assign variables like that, you need the SET keyword first.
http://dev.mysql.com/doc/refman/5.1/en/set-statement.html
So you code should be something like this (tested with phpMyAdmin):
DELIMITER //
CREATE PROCEDURE lastscan(IN task_id_var INT)
BEGIN
SELECT COUNT(*) FROM debugger WHERE task_id=task_id_var INTO #total;
SET #total=#total+1;
INSERT INTO debugger SET scan_num=#total, task_id=task_id_var;
END;//
DELIMITER ;
The DELIMITER keyword is used to stop additional semicolons in your procedure to be the end of the current statement, so by redefining the delimiter to // MySql will process the whole CREATE PROCEDURE-block as one single statement and not stop at the first semicolon but instead wait for the first occurrence of //.
http://dev.mysql.com/doc/refman/5.1/en/stored-programs-defining.html

SQL syntax error when creating a stored procedure in MySQL

I have a hard time locating an error when trying to create a stored procedure in mysql.
If I run every single line of the procedure independently, everything works just fine.
CREATE PROCEDURE cms_proc_add_child
(
param_parent_id INT, param_name CHAR(255),
param_content_type CHAR(255)
)
BEGIN
SELECT #child_left := rgt FROM cms_tree WHERE id = param_parent_id;
UPDATE cms_tree SET rgt = rgt+2 WHERE rgt >= #child_left;
UPDATE cms_tree SET lft = lft+2 WHERE lft >= #child_left;
INSERT INTO cms_tree (name, lft, rgt, content_type) VALUES
(
param_name,
#child_left,
#child_left+1,
param_content_type
);
END
I get the following (helpful) error:
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
I just don't know where to start debugging, as every single one of these lines is correct.
Do you have any tips?
As line 3 contains the first ; perhaps you have a problem with your delimiters.
See http://dev.mysql.com/doc/refman/en/stored-programs-defining.html
DELIMITER //
CREATE PROCEDURE dorepeat(p1 INT)
BEGIN
SET #x = 0;
REPEAT SET #x = #x + 1; UNTIL #x > p1 END REPEAT;
END//
DELIMITER ;
Thanks, near '' at line 3 was my problem and the delimiter statement fixed it! I always want things to make sense and this does. As the '' indicates it's at the end of the procedure, but no END statement was found thus the syntax error. And I wondered why I kept seeing a lot of people using the delimiter statement. I see the light!
You never declare your #child_left variable.
If you having issues with a bunch of Procedure that can't run at the same time but can run successfully alone, Try separate them with Go command.
Ex:
--i)
CREATE PROCEDURE A
AS
BEGIN
END;
GO
--ii)
CREATE PROCEDURE B
AS
BEGIN
END;