can't create mysql stored procedure - mysql

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.

Related

How do I troubleshoot this stored procedure?

I wrote a stored procedure, but no matter what I do, the error does not go away.
The MySQL error is:
#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 'DECLARE cr CURSOR For (SELECT id,subtitle,price FROM book)
...' at line 4
The code I wrote is as follows:
‍‍‍```
DELIMITER //
CREATE PROCEDURE show_book()
BEGIN
DECLARE #id int(11), #subtitle varchar(30), #price int(7)
DECLARE cr CURSOR For SELECT id,subtitle,price FROM book
OPEN cr
FETCH NEXT FROM cr INTO #id,#subtitle,#price
WHILE(##FETCH_STATUS=0)
BEGIN
Print(#id + ' '+ #subtitle + ' '+ Cast(#price as varchar(7)))
FETCH NEXT FROM cr INTO #id,#subtitle,#price
END
CLOSE cr
DEALLOCATE cr
END //
DELIMITER ;
Things to fix:
In MySQL local variables do not use the #-prefix.
Use separate DECLARE for each variable
No need to use int(11)/int(7), just use int. They are all the same.
You should terminate each statement with a semicolon
See the documentation.

Use cursor within stored procedure [duplicate]

This question already has answers here:
syntax error for mysql declaration of variable
(2 answers)
Closed 4 years ago.
I'm trying to make a stored procedure that include a cursor inside it and fill one of my tables based on another table's data , every day .
I think I'm doing something wrong with syntax , I already wrote a simple Stored procedure with cursor and it worked totally right , but when it get a little more complicated it does not work any more .
I'm getting
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 'DECLARE brandId int ;' at line 1.
Please note that I'm using Mysql 5.7 and I'm creating this at phpmMyAdmin .
CREATE PROCEDURE ّFillCommentGrowth()
BEGIN
DECLARE brandId int;
DECLARE todayComment int ;
DECLARE brandCount int ;
DECLARE yesterdayComment int;
DECLARE crs CURSOR for SELECT id from brands;
SET brandCount = (SELECT count(*) from brands);
open crs;
WHILE brandCount > 0 DO
FETCH crs into brandId ;
set todayComment = (select IFNULL((select count(*) from comments as c where date(c.created_at) = date(subdate(NOW(),1)) and c.brand_id = brandId ),0));
set yesterdayComment = (select IFNULL((select commentAmount from commentsGrowth where moment = date(subdate(NOW(),2)) and brand_Ref= brandId),0));
INSERT INTO commentsGrowth
(
brand_Ref,
commentAmount,
diffrenceByYesterday,
degree,
AmountPercent,
moment)
VALUES
(brandId ,
todayComment,
(todayComment - yesterdayComment ) ,
(((ATAN(todayComment - yesterdayComment )*180))/PI()),
(degree*(1.1)),
date(subdate(NOW(),1)));
SET brandCount = brandCount - 1;
END WHILE;
close crs;
END
The error you are getting has nothing to do with cursor. You need to change the DELIMITER from standard semicolon (;). For example
DELIMITER //
CREATE PROCEDURE GetAllProducts()
BEGIN
SELECT * FROM products;
END //
DELIMITER ;
The DELIMITER statement changes the standard delimiter which is semicolon ( ; ) to another. In this case, the delimiter is changed from the semicolon( ; ) to double-slashes //. Why do we have to change the delimiter? Because we want to pass the stored procedure to the server as a whole rather than letting mysql tool interpret each statement at a time. Following the END keyword, we use the delimiter // to indicate the end of the stored procedure. The last command ( DELIMITER; ) changes the delimiter back to the semicolon (;).

Mysterious error in CREATE PROCEDURE in MariaDB/MySQL

I tried to make a simple procedure in MariaDB 10.2 but I encountered an issue regarding variables defining.
I am receiving (conn:107) 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 message when I declare a variable.
I read the MariaDB documentation and I it says that a variable is defined like this DECLARE var_name [, var_name] ... type [DEFAULT value]
Where I am wrong? I am coming from Oracle SQL and some sintax is wired for me.
I use Eclipse with MariaDB JDBC to connect on SQL.
CREATE PROCEDURE nom_jobs_insert(IN p_name varchar(100) CHARACTER SET 'utf8')
BEGIN
DECLARE counter INT DEFAULT 0;
SELECT count(*) INTO counter
FROM nom_jobs
WHERE lower(name) = lower(p_name)
IF counter = 1 THEN
INSERT INTO nom_jobs(name) VALUES (p_name);
END IF;
END;
I found the solution.
In MariaDB you have to define a delimiter before create a procedure and you need to mark where the procedure code is finished.
DELIMITER //
CREATE PROCEDURE nom_jobs_insert(IN p_name varchar(100) CHARACTER SET 'utf8')
BEGIN
DECLARE counter INT DEFAULT 0;
SELECT count(*) INTO counter
FROM nom_jobs
WHERE lower(name) = lower(p_name);
IF counter = 1 THEN
INSERT INTO nom_jobs(name) VALUES (p_name);
END IF;
END; //
You have error not in DECLARE expression, add ; after SELECT statement
Here are the clues that point to a missing DELIMITER:
near '' at line 3
Line 3 contains the first ;
When the error says near '', the parser thinks it has run off the end of the "statement".
Put those together -- it thinks that there is one 3-line statement ending with ;. But the CREATE PROCEDURE should be longer than that.
CREATE PROCEDURE nom_jobs_insert(IN p_name varchar(100) CHARACTER SET 'utf8')
IS
DECLARE counter INTEGER DEFAULT 0;
BEGIN
SELECT count(*) INTO counter
FROM nom_jobs
WHERE lower(name) = lower(p_name)
IF counter = 1 THEN
INSERT INTO nom_jobs(name) VALUES (p_name);
END IF;
END;

Error when using case with variables

Hey guys i am facing a problem when i use a variable with a case in mysql.
The code which i have used is
DECLARE vSite VARCHAR(20);
SET vSite = case
when id > 0 then 'sdfsdf'
else 'asd' end as name
from customers;
When i run this code it throws me error like
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 vSite VARCHAR(20)' at line 1: DECLARE vSite VARCHAR(20)
CAN anyone point me where am going wrong..Thanks for your valuable help
You need to declare variables inside a BEGIN END block.
Here is a simple example of a stored procedure
DELIMITER //
CREATE procedure blah(IN customer_id INT,OUT vSite VARCHAR(20))
BEGIN
SELECT CASE WHEN id > 0 THEN 'blah'
ELSE 'mah' END INTO vSite FROM customers WHERE id=customer_id;
END//
DELIMITER ;
CALL blah(3,#somevar);
SELECT #somevar;

Why do SQL declare fail?

I have this piece of code:
BEGIN
DECLARE #NewLine CHAR(2)
SET #NewLine = CHAR(13)+CHAR(10)
END
When I run it in phpmyadmin, I get 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 'DECLARE #NewLine CHAR(2) SET #NewLine = CHAR(13) + CHAR(10) END' at line 2
I used about 2 hours on Google, without any luck. As far as I can tell, it should be the right syntax.
Someone please help
Flow statements like IF, WHEN and so on are only allowed in stored procedures or function in MySQL. BEGIN and END are delimiters of such statements.
Put that code in a procedure and it should work.
Edit
Example procedure
DELIMITER //
CREATE PROCEDURE newline_proc(input varchar(1000))
BEGIN
declare newline char(2);
select CHAR(13)+CHAR(10) into newline;
...
END//
DELIMITER ;