Why do SQL declare fail? - mysql

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 ;

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.

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;

Syntax error in MySQL Function

I'm trying to set up a MySQL function for my Mail-server. The MySQL Version is 5.1.66.
I do know what is wrong with this query. I also tried with RETURN DOUBLE, READS SQL DATA, and DETERMINISTIC but none of them help.
I am using PhpMyAdmin. The delimiter is set to $$. But all I get is a cryptic error message:
#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 'TEXT CHARSET utf8 READS SQL DATA BEGIN DECLARE mygoto VARCHAR(25' at line 3
My code:
CREATE PROCEDURE `get_email_alias`(
myemail VARCHAR(255)
) RETURNS TEXT CHARSET utf8
READS SQL DATA
BEGIN
DECLARE mygoto VARCHAR(255);
DECLARE sdomain VARCHAR(255);
DECLARE ddomain VARCHAR(255);
SELECT SUBSTRING(myemail, INSTR(myemail, '#')+1) INTO sdomain;
SELECT target_domain
FROM alias_domain
WHERE alias_domain = sdomain
AND active = 1
LIMIT 1
INTO ddomain;
IF ddomain IS NOT NULL THEN
SELECT REPLACE(myemail, sdomain, ddomain) INTO myemail;
END IF;
SELECT goto
FROM alias
WHERE address = myemail
AND active = 1
LIMIT 1
INTO mygoto;
IF mygoto IS NOT NULL THEN
RETURN mygoto;
END IF;
RETURN null;
END $$
For anyone that comes across this later:
There was originally a syntax error in the keyword PROCEDURE. It was missing the final E.
According to the MySQL syntax, CREATE PROCEDURE does not RETURN. However, CREATE FUNCTION does allow the RETURN in the syntax. Reference: http://dev.mysql.com/doc/refman/5.1/en/create-procedure.html.
PROCEDURE" on first line is missing "E"

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.