Hex number literal in MySQL stored function - mysql

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

Related

Mariadb: assignment operator (:=) not working with local variables

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?

How to write a function in MySQL and check that it works

I'm trying to write a simple function in MySQL.
DROP FUNCTION IF EXISTS BINOMIAL;
DELIMITER $$
CREATE FUNCTION BINOMIAL(g VARCHAR, s VARCHAR) RETURNS VARCHAR
BEGIN
DECLARE x VARCHAR;
set x = CONCAT(g, s);
RETURN x;
END$$;
DELIMITER ;
SELECT BINOMIAL(Accessions.Genus, Accessions.Species) FROM Accessions;
I can't tell what I'm doing wrong and I can't find any good/understandable guides on function creation.
PhpMyAdmin throws 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 'LIMIT 0, 30' at line 2".
From what I undersstand, the SELECT statement doesn't work because the function isn't being created.

Wrong MYSQL Create Function syntax?

I'm trying to create a function like this in MYSQL:
DELIMITER $$
CREATE FUNCTION `submit`(title VARCHAR(45)) RETURNS INT
BEGIN
DECLARE articleId INT;
INSERT INTO Articles (`Title`) VALUES (title);
SET #articleId = LAST_INSERT_ID();
RETURN (articleId);
END
$$ DELIMITER ;
No matter how I change it (including removing everything in the body and placing a "return 1;" instead) I get:
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 $$ CREATE FUNCTION submit(title v' at line 1
What am I missing?
I'm using MySQL 5.5
EDIT
Apparently, MySQL is ignoring the first statement (DELIMITER $$), failing on the first ; it finds, right after articleId INT in the 4th line. I had to reduce the code to the shortest form possible to make sure that's the case.
So I guess my question now is - Why is the DELIMITER keyword ignored?

mysql 5.1: how can i use benchmark() command to test a call to a stored procedure?

I'm trying to benchmark a stored procedure.
select benchmark(100000000,(select 1));
this benchmark works
but the following benchmark doesn't:
do benchmark(1000,(call test_login_user('a')));
it produces the following 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 'call xpofb_login_user('a')))' at line 1
any ideas how to resolve the issue ?
You can't do this with benchmark(), but you could create a stored procedure to do it.
Here's an example:
delimiter $$
create procedure benchmark_test_login_user (p_username varchar(100),
p_count int unsigned)
begin
declare v_iter int unsigned;
set v_iter = 0;
while v_iter < p_count
do
call test_login_user(p_username);
set v_iter = v_iter + 1;
end while;
end $$
delimiter ;
call benchmark_test_login_user('a',1000);
You can't
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_benchmark
Only scalar expressions can be used. Although the expression can be a subquery, it must return a single column and at most a single row. For example, BENCHMARK(10, (SELECT * FROM t)) will fail if the table t has more than one column or more than one row.

Can any one suggest me how to resolve this problem: Error Code : 1064 in MY SQL 5.5 ver

DELIMITER $$;
DROP FUNCTION IF EXISTS tonumeric $$;
CREATE FUNCTION tonumeric() returns numeric
BEGIN
declare num numeric;
set num = to_number('12');
return num;
END$$
DELIMITER; $$
When I executed this function, I am facing this error.
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 'IF EXISTS tonumeric' at line 1
(0 ms taken)
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 ';
CREATE FUNCTION tonumeric() returns numeric
BEGIN
declare num numeric;
set num' at line 1
(0 ms taken)
Thanks
How about this:
DELIMITER $$
DROP FUNCTION IF EXISTS tonumeric $$
CREATE FUNCTION tonumeric() returns numeric
BEGIN
declare num numeric;
set num = to_number('12');
return num;
END$$
DELIMITER ;
Delimiter is a special command, in that you shouldn't terminate it with a ; -- you're actually setting the delimiter to "$$;", not "$$".