I have problem with declare variable in stored procedure. I used MySQL. My example code:
CREATE PROCEDURE `name`()
BEGIN
DECLARE varname INT;
SELECT * FROM `table` INTO var;
END
MySQL returns error:
error 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 3
When you have multiple statements in a procedure you have to change the delimiter. Otherwise MySQL thinks that the procedure declaration is finished after the first statement.
Here's an example that works. And btw, var is not a reserved keyword like others are trying to tell you.
DELIMITER $$
CREATE PROCEDURE test ()
BEGIN
DECLARE var INT;
SELECT 1 INTO var;
SELECT var;
END $$
DELIMITER ;
CALL test();
+------+
| var |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
var is a reserved key word
CREATE PROCEDURE test ()
BEGIN
DECLARE var2 INT;
// Do something
END;
Update :
I saw MAriaDB in the error , the correct way to declare variable in MariaDB is
SET #var = 0;
You should tag your question with the correct keywords
Related
I'm writing an script to update a db schema, the script is the following:
delimiter //
begin
set #check_exists = 0;
select count(*)
into #check_exists
from information_schema.columns
where table_schema = 'my_app'
and table_name = 'users'
and column_name = 'points';
if (#check_exists = 0) then
alter table my_app.users
add points int not null default 0
end if;
end //
delimiter
when I run it, i get the below error:
Reason:
SQL Error [1064] [42000]: (conn=34) 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 'set #check_exists = 0;
I have already checked the answers the below two posts but none solves my problem.
this question where the solution was to change the delimiter, and to
this question where the solution was to remove the DECLARE keyword and just declare the variable as it is in the MariaDB manual here with set #var int;
I think you need to have a CREATE PROCEDURE before your BEGIN statement. So:
delimiter //
CREATE PROCEDURE UPDATE_SCHEMA()
begin
set #check_exists = 0;
(...your other script here...)
end//
delimiter ;
call UPDATE_SCHEMA();
An additional note: check_exists in your if statement needs a #
According to the BEGIN END documentation here, the syntax is:
BEGIN [NOT ATOMIC]
[statement_list]
END [end_label]
And here is what I did not know:
NOT ATOMIC is required when used outside of a stored procedure. Inside stored procedures or within an anonymous block, BEGIN alone starts a new anonymous block.
As I'm not using the BEGIN END in a stored procedure, I have to add the NOT ATOMIC after the BEGIN keyword.
So the code should look like this:
delimiter //
begin not atomic
set #check_exists = 0;
-- rest of the code here...
The Mariadb Knowledge base explains that the assignment operator (:=) works with both user-defined variables and local variables.
But when I make some tests, for example :
delimiter //
CREATE or replace PROCEDURE tst_PS_now()
BEGIN
declare varnow datetime(3);
set varnow = (select now());
select 'result: ' ,varnow;
END;
//
delimiter ;
mysql:root> call tst_PS_now() ;
+----------+-------------------------+
| result: | varnow |
+----------+-------------------------+
| result: | 2020-10-26 16:13:44.000 |
+----------+-------------------------+
example 2:
delimiter //
CREATE or replace PROCEDURE tst_PS_now()
BEGIN
declare varnow datetime(3);
select varnow:=now();
select 'result: ' ,varnow;
END;
//
delimiter ;
ERROR 1064 (42000): 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 ':=now(); at line 4
In the second example, the assignment should work with my local variable.
I really need this kind of assignment in my stored procedures.
How may I fix it ? Any idea?
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;
I am trying to create a function that returns rowcount. But it returns error again and again.
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 11
DELIMITER $$
CREATE FUNCTION func1(userid INT)
RETURNS INT
NOT DETERMINISTIC
BEGIN
DECLARE var_name INT;
SET var_name = 0;
SELECT COUNT(*) INTO var_name
FROM wps_bal
WHERE u_id = userid;
RETURN var_name;
END$$
Most probably your version of PHPMyAdmin does not support the DELIMITER statement which is not MySQL statement. Here you can find how to create the function in PHPMyAdmin: Store procedures in phpMyAdmin
Yes. This was helpfull
I have created the solution:
DELIMITER $$
CREATE FUNCTION func1(userid INT)
RETURNS INT
NOT DETERMINISTIC
BEGIN
DECLARE var_name INT;
SET var_name = 0;
SELECT COUNT(*) INTO var_name
FROM wps_bal
WHERE u_id = userid;
RETURN var_name;
END$$
DELIMITER ;
Can anyone see why I would get:
[Err] 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 '//
DELIMITER' at line 11
For the following snippet:
DELIMITER //
DROP PROCEDURE
IF EXISTS nested_test//
CREATE PROCEDURE nested_test()
BEGIN
DECLARE a INT;
SET a = 1;
SELECT a;
BEGIN
DECLARE b INT;
SET b = 2;
SELECT b;
END;
END//
DELIMITER;
This is a simplified version of what I am actually writing and it brings up exactly the same error. Everything is great until I add the nested BEGIN END block. It works perfectly well on phpMyAdmin but fails on Navicat 9
It should have a space after DELIMITER.
DELIMITER ;
Like that.
DELIMITER //
DROP PROCEDURE
IF EXISTS nested_test//
CREATE PROCEDURE nested_test()
BEGIN
DECLARE a INT;
SET a = 1;
SELECT a;
BEGIN
DECLARE b INT;
SET b = 2;
SELECT b;
END;
END //
DELIMITER ;
On execution, it generates:
0 rows affected, 0 rows found. Duration for 2 queries: 0.000 sec.