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?
Related
mysql> delimiter &&
mysql> create procedure get()
-> begin
-> select * from a;
-> end&&
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 'get()
begin
select * from a;
end' at line 1
mysql> show tables;
+----------------+
| Tables_in_data |
+----------------+
| a |
| test |
+----------------+
This is because GET is a reserved word in MySql, and therefore cannot be used as an object name without using backticks.
The following will work:
delimiter &&
create procedure `get`()
begin
select * from a;
end&&
delimiter ;
Alternatively, use another name for the procedure (which would be my recommendation as using reserved words as object names is unwise).
mysql> show tables;
+---------------------+
| Tables_in_cpsc408db |
+---------------------+
| Product |
| laptop |
| pc |
| printer |
+---------------------+
4 rows in set (0.00 sec)
mysql> create procedure hello()
-> begin
-> select * from product;
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
mysql>
I am not sure what is causing this syntax error, and haven't had any success so far figuring it out. Any help would be much appreciated.
You need to use the DELIMITER keyword:
This is so MySQL can tell which statements are within the procedure and where the end of the procedure declaration itself is
delimiter //
CREATE PROCEDURE hello()
BEGIN
select * from product;
END//
delimiter ;
You need a delimiter otherwise the console does not know when you are finished:
DELIMITER //
CREATE PROCEDURE hello()
BEGIN
SELECT * FROM product;
END //
DELIMITER ;
Read more about it here: Getting Started with MySQL Stored Procedures
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
I want to use hexadecimal literals in the code of a MySQL function that does operations with bit masks on BIGINTs.
According to the MySQL 5.6 manual, I can use hexadecimal literals for integer values.
This works from an SQL statement in MySQL 5.6:
mysql> SELECT CAST(0x0055aa55aa55aaff AS UNSIGNED);
+--------------------------------------+
| CAST(0x0055aa55aa55aaff AS UNSIGNED) |
+--------------------------------------+
| 24112657927088895 |
+--------------------------------------+
1 row in set (0.01 sec)
I'm now trying to use the same syntax from a stored function, on the same MySQL:
DROP FUNCTION IF EXISTS _test_hex_literal;
DELIMITER $$
CREATE FUNCTION _test_hex_literal()
RETURN BIGINT UNSIGNED
DETERMINISTIC
NO SQL
BEGIN
RETURN CAST(0x0055aa55aa55aaff AS UNSIGNED);
END;
$$
DELIMITER ;
SELECT _test_hex_literal();
DROP FUNCTION IF EXISTS _test_hex_literal;
But MySQL 5.6 is rejecting my code:
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 'RETURN BIGINT UNSIGNED
DETERMINISTIC
NO SQL
BEGIN
RETURN CAST(0x0055' at line 2
Am I doing something wrong, or is it a bug in MySQL?
If this is a MySQL bug, is there a workaround to use hexadecimal constants in the source code of my function?
Simple typo: I wrote RETURN instead of RETURNS in the function signature.
Here is the fixed code:
DROP FUNCTION IF EXISTS _test_hex_literal;
DELIMITER $$
CREATE FUNCTION _test_hex_literal()
RETURNS BIGINT UNSIGNED
DETERMINISTIC
NO SQL
BEGIN
RETURN CAST(0x0055aa55aa55aaff AS UNSIGNED);
END;
$$
DELIMITER ;
SELECT _test_hex_literal();
DROP FUNCTION IF EXISTS _test_hex_literal;
The error complaints about RETURN, not hexadecimal literals:
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 'RETURN BIGINT UNSIGNED
DETERMINISTIC
NO SQL BEGIN
RETURN CAST(0x0055'
Here's the syntax:
CREATE
[DEFINER = { user | CURRENT_USER }]
FUNCTION sp_name ([func_parameter[,...]])
RETURNS type
^
[characteristic ...] routine_body
Is it possible to use SELECT local_variable := expression FROM [...] syntax in MySQL with local variables as opposed to session variables?
This works:
DELIMITER //
DROP PROCEDURE IF EXISTS test_local_variables;
CREATE PROCEDURE test_local_variables()
BEGIN
select #last_name, #last_name := SPECIFIC_NAME from information_schema.ROUTINES limit 10;
END;
//
DELIMITER ;
However, I can't get MySQL to take this:
DELIMITER //
DROP PROCEDURE IF EXISTS test_local_variables;
CREATE PROCEDURE test_local_variables()
BEGIN
DECLARE last_name VARCHAR(18);
SET last_name = 'first_value';
select last_name, last_name := SPECIFIC_NAME from information_schema.ROUTINES limit 10;
END;
//
DELIMITER ;
It fails with the 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 ':= SPECIFIC_NAME from information_schema.ROUTINES
limit 10; END' at line 6
I'd really like to get this working with local variables. Thanks!