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.
Related
Does anyone see where the mistake can be? been dealing with this one all afternoon. Workbench isn't very useful in pointing out the mistakes.
It appears the next mistake in my code
CALL pCityMean('Spain') Error Code: 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 2.
DELIMITER $$
DROP PROCEDURE IF EXISTS pCityMean $$
CREATE PROCEDURE pCityMean (IN vPais VARCHAR(30))
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE vID INT;
DECLARE cur1 CURSOR FOR SELECT distinct id_city FROM world_temp_stats.temp_by_city where id_country=(SELECT id_country FROM world_temp_stats.country where Name=vPais);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur1;
bucle1: LOOP
FETCH cur1 INTO vID;
IF done = 1 THEN
LEAVE bucle1;
END IF;
SET #nomCiutat = (SELECT name FROM world_temp_stats.city WHERE id = vID);
SET #tablaCiutat = CONCAT ("SELECT year(dt), count(AverageTemperature), ROUND(avg(AverageTemperature), 2) INTO OUTFILE '",UCASE(vPais),"_",UCASE(#nomCiutat),"_temp_analisis.csv' FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\n' FROM world_temp_stats.temp_by_city where id_country=(SELECT id_country FROM world_temp_stats.country where Name='",vPais,"' and id_city=",vID," group by year(dt)");
PREPARE execucio FROM #tablaCiutat;
EXECUTE execucio;
DEALLOCATE PREPARE execucio;
END LOOP bucle1;
CLOSE cur1;
END $$
DELIMITER ;
CALL pCityMean('Spain');
I can eyeball your query and I see that you forgot a closing parenthesis.
SET #tablaCiutat = CONCAT ("SELECT ...
where id_country=(
SELECT id_country FROM world_temp_stats.country
where Name='",vPais,"' and id_city=",vID," group by year(dt)
");
I think you got mixed up because year(dt) ends in a right-paren, but you need one more to close the subquery. Something like the following:
SET #tablaCiutat = CONCAT ("SELECT ...
where id_country=(
SELECT id_country FROM world_temp_stats.country
where Name='",vPais,"' and id_city=",vID," group by year(dt)
)
");
You should also use query parameters for dynamic values in your prepared query, instead of string-concatenation.
But if it were me, I would change so many things about this code. Why use INTO OUTFILE, and why use a stored procedure at all? In my experience, MySQL stored procedures are more difficult than writing the code in virtually any client programming language. I hardly ever use MySQL stored procedures.
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;
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"
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 ;
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.