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!
Related
Failed to run followwing code in mysql 8.0.
drop table if exists test ;
create table if not exists test(rowid int);
delimiter$$
drop procedure if exists line_sum $$
create procedure line_sum()
begin
declare i int ;
declare exe_sql varchar(100);
set i=5 ;
while i>0 do
set exe_sql = concat('alter table test add column d ',i,' int') ;
prepare ppsql from exe_sql ;
execute ppsql ;
deallocate prepare ppsql ;
set i = i-1;
end while ;
end$$
delimiter ;
It reported:
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 'while i>0 do
set exe_sql = concat('alter table test add column d ',i,' int'
at line 11
However my colleague and I have checked it many times, finding no issues in the syntax and getting more confusion.
simple question
this is the code to creat a simple stored procedure
DELIMITER $$
CREATE PROCEDURE `check_mobile_sp`(
IN mobile_numberEntered VARCHAR(12)
)
BEGIN
SELECT id, mobile_number, `date`
from users
WHERE mobile_number = mobile_numberEntered;
END $$
DELIMITER ;
and this is the 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 'DELIMITER' at line 1
try placing // instead of $$:
DELIMITER //
CREATE PROCEDURE `check_mobile_sp`(
IN mobile_numberEntered VARCHAR(12)
)
BEGIN
SELECT id, mobile_number, `date`
from users
WHERE mobile_number = mobile_numberEntered;
END //
DELIMITER ;
I'm creating procedure which is having two parameters , one is p_cursor of type SYS_REFCURSOR (OUT param) and the other one is p_rank of type INT(IN param). But it showing an error.
DELIMITER $$
CREATE PROCEDURE sp_student(p_cursor OUT SYS_REFCURSOR,p_rank IN INT)
BEGIN
OPEN p_cursor FOR SELECT * FROM student WHERE rank = p_rank;
END$$
DELIMITER ;
the error what I'm getting is,
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 'OUT SYS_REFCURSOR,p_rank IN INT)
BEGIN
OPEN p_cursor FOR SELECT * FROM st' at line 1
I think I'm syntactically wrong for SYS_REFCURSOR.. please check my code and let me realise my mistake.
thanks in advance
mysql doesnt have refcursor like oracle, if u r planning to write a stored procedure that returns multiple rows/result set in mysql just do
DROP procedure IF EXISTS `sample`;
DELIMITER $$
CREATE PROCEDURE `sample`(p_rank IN INT)
BEGIN
select * from MyTable where id=p_rank;
END$$
DELIMITER ;
call sample();
this will return a result set. which u can use.
I am executing below procedure in Mysql.
DROP procedure IF EXISTS `wm_batch_list`;
DELIMITER $$
/*!50003 SET #TEMP_SQL_MODE=##SQL_MODE, SQL_MODE='STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ $$
CREATE DEFINER=`root`#`localhost` procedure `wm_batch_list`(IN p_start int(11),IN p_range int(11))
BEGIN
select * from batch ORDER BY start_year DESC limit p_start,p_range;
END $$
/*!50003 SET SESSION SQL_MODE=#TEMP_SQL_MODE */ $$
DELIMITER ;
but i am getting this error:
Error code 1064, SQL state 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 'p_start,p_range;
END' at line 3
Please suggest where i am doing mistake?
Googling a bit i found a question related to yours at passing LIMIT as parameters to MySQL sproc
From http://dev.mysql.com/doc/refman/5.1/en/select.html:
The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants (except when using prepared statements).
So the way to solve it is using a prepare statement
SET #skip=p_start;
SET #rows=p_range;
PREPARE STMT FROM 'SELECT * FROM table LIMIT ?, ?';
EXECUTE STMT USING #skip, #rows;
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.