Can't figure out stored procedure, error at END - mysql

I was hoping you could help me out...
I do know some SQL, but I'm new to mySQL... and there's this simple query that I just can't figure out what's wrong with it:
CREATE PROCEDURE inserttoscrapbookSD
(owner1 VARCHAR(50),
poster1 VARCHAR(50),
Scrap1 VARCHAR(50),
)
BEGIN
INSERT INTO scrapbook (Owner)
VALUES(owner1)
END
I know there are a lot of variables being passed, but just using one variable at the moment because if it works for one, it'll work for all. I tried with and without a semicolon ( ; ) at the end of END and VALUES(owner1) but no luck. It says:
#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 INSERT INTO scrapbook (Owner) VALUES(owner1) END' at
line 6

Your problem is you need to change the delimiter while you define the stored proc, which allows you to use semicolons ; within your stored proc code without finishing the create command.
Try this:
delimiter //
CREATE PROCEDURE inserttoscrapbookSD (
owner1 VARCHAR(50),
poster1 VARCHAR(50),
Scrap1 VARCHAR(50)
)
BEGIN
INSERT INTO scrapbook (Owner)
VALUES(owner1);
END
//
delimiter ;

Try removing the comma after the last parameter

Related

MySQL query unknown error

I'm trying to run this query on MySQL server:
CREATE PROCEDURE forum.eventlog_create(
i_UserID INT,
i_Source VARCHAR(128),
i_Description TEXT,
i_Type INT,
i_UTCTIMESTAMP DATETIME)
MODIFIES SQL DATA
BEGIN
INSERT INTO forum.EventLog
(UserID, Source, Description, ´Type´)
VALUES (i_UserID, i_Source, i_Description, i_Type);
END;
However upon executing it 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 12
and I'm unable to fix it. I tried to search for a solution and asked a co-worker but we were unable to find the solution, as last resort I decided to ask it here.
I get error code 1064 but the right syntax near '' is the message and I dont understand what the problem could be. It would be easier if it said which syntax gives the error, I only get the line number.
Thank you for your time
There is one error caused by the escape character around type which should be either backticks or dropped and you should try setting delimiters https://dev.mysql.com/doc/refman/5.7/en/stored-programs-defining.html
delimiter $$
CREATE PROCEDURE eventlog_create(
i_UserID INT,
i_Source VARCHAR(128),
i_Description TEXT,
i_Type INT,
i_UTCTIMESTAMP DATETIME)
MODIFIES SQL DATA
BEGIN
INSERT INTO forum.EventLog
(UserID, Source, Description, `Type`)
VALUES (i_UserID, i_Source, i_Description, i_Type);
END $$
delimiter ;
Try this
DELIMITER $$
CREATE PROCEDURE forumeventlog_create()
BEGIN
declare i_UserID INT DEFAULT 0;
declare i_Source VARCHAR(128) DEFAULT null;
declare i_Description TEXT DEFAULT null;
declare i_Type INT DEFAULT 0;
declare i_UTCTIMESTAMP DATETIME DEFAULT null ;
INSERT INTO forum.EventLog
(UserID, Source, Description, ´Type´)
VALUES (i_UserID, i_Source, i_Description, i_Type);
END $$
DELIMITER ;
You can call this by
CALL forumeventlog_create()
OR
DELIMITER $$
CREATE PROCEDURE forumeventlog_create(i_UserID INT,i_Source VARCHAR(128),i_Description TEXT,i_Type INT,i_UTCTIMESTAMP DATETIME)
BEGIN
INSERT INTO forum.EventLog
(UserID, Source, Description, ´Type´)
VALUES (i_UserID, i_Source, i_Description, i_Type);
END $$
DELIMITER ;

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.

can't create mysql stored procedure

I'm a bit of a noob when it comes to stored procedures in general, but I'm trying to write the following
Create procedure clone_perms
as
declare #new_id varchar(30), #old_id varchar(30)
declare get_perms cursor for select userspermsUserid, userspermsPermission from users_permissions where userspermsUserid=#old_id
declare #perms varchar(30), #on_off boolean
FETCH get_perms into #perms, #on_off
while(##sqlstatus=0)
BEGIN
if exists ( select 1 from permissions where userspermsUserid=#new_id and userspermsPermID=#perm )
BEGIN
update permissions set userspermsPermission=#on_off where userspermsUserid=#new_id and userspermsPermID=#perm
END
else
BEGIN
insert permissions (userspermsUserID, userspermsPermID, userspermsPermission) values (#new_id, #perms, #on_off)
END
FETCH get_perms into #perms, #on_off
END
CLOSE get_perms
DEALLOCATE CURSOR get_perms
end
. I get the following error when trying to create it:
/* SQL 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 'as declare #new_id varchar(30) declare #old_id varchar(30) declare get_perm' at line 2 */
. Does anyone know what I need to do to make this work?
You need to have BEGIN tag after CREATE PROCEDURE clone_perms and not AS
don't use an # to start local (declared) variable names, that's only for user variables you create with the
SET #varname=value;
statement. you'll also need to terminate your statements with a semicolon. that's the cause of the latest error, there's no ; after your first declare statement.

Mysql stored procedure error

I'm trying to add the following stored procedure to my mysql database:
CREATE PROCEDURE logmsg ( _Username VARCHAR(50), _Message VARCHAR(80) )
BEGIN
INSERT INTO chatlogs (sender, message) VALUES (_Username, _Message);
END;
But its failing the query and returning:
#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've been searching for about 2 hours on google and cannot find any answer at all.
Any help is greatly appreciated!
While I'm not 100% sure because I can't test on a MySQL server at the moment, I think the problem is in the semicolon. On the line with INSERT you basically end the CREATE PROCEDURE statement, which has incorrect syntax this way. You have to set the delimiter to something else (e.g. //), to be able to use the semicolon in the body of the procedure:
delimiter //
CREATE PROCEDURE logmsg ( _Username VARCHAR(50), _Message VARCHAR(80) )
BEGIN
INSERT INTO chatlogs (sender, message) VALUES (_Username, _Message);
END//
delimiter ;

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;