MySQl Query with loops without creating as procedure - mysql

Can we execute this loop without creating them as a procedure or function, in the direct query window.
DELIMITER $$;
BEGIN
DECLARE counts INT;
SET counts:=0;
WHILE counts < 10 DO
Select counts;
SET counts = counts + 1;
END WHILE;
END $$
DELIMITER ;
When I tried, it does not execute and resulted with below exception :
Unknown system variable 'counts'
Expectation :
while developing need to do certain loops as per logic defined. to ensure it is working without error. need to test the logic without creating as a procedure.
we have option in Postgres to execute the procedure statement by DO.
DO $$
DECLARE
counts INT default 0;
BEGIN
WHILE counts < 10 loop
raise notice 'count : %' , counts;
counts:= counts + 1;
END loop;
END
$$;
Result :
NOTICE: count : 0
NOTICE: count : 1
NOTICE: count : 2
NOTICE: count : 3
NOTICE: count : 4
NOTICE: count : 5
NOTICE: count : 6
NOTICE: count : 7
NOTICE: count : 8
NOTICE: count : 9
DO
Query returned successfully in 210 msec.
postgres result

Related

Writing script creates and calls a stored procedure

Write a script that creates and calls a stored procedure named test. This stored procedure should
declare a variable and set it to the count of all products in the Products table. If the count is greater
than or equal to 7, the stored procedure should display a message that says, “The number of products
is greater than or equal to 7”. Otherwise, it should say, “The number of products is less than 7”.
DROP PROCEDURE IF EXISTS test;
CREATE procedure test()
BEGIN
DECLARE count_of_7 DECIMAL(10,2);
SELECT count(product_id)
into count_of_7
FROM products;
IF count_of_7 >= 7 THEN
SELECT 'The number of products is greater than or equal to 7' AS message;
ELSE
SELECT 'The number of products is less than 7' AS message;
end if;
call test();
21:38:55 CREATE procedure test() BEGIN DECLARE count_of_7 DECIMAL(10,2) 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 '' at line 3 0.016 sec
You need to change the delimiter before declaring your procedure. Also, you are missing an END statement. This should work:
DROP PROCEDURE IF EXISTS test;
DELIMITER //
CREATE procedure test()
BEGIN
DECLARE count_of_7 DECIMAL(10,2);
SELECT count(product_id) into count_of_7 FROM products;
IF count_of_7 >= 7 THEN
SELECT 'The number of products is greater than or equal to 7' AS message;
ELSE
SELECT 'The number of products is less than 7' AS message;
END IF;
END //
DELIMITER ;
call test();

How to limit execution time in MySQL?

I have following code to limit execution time in MySQL but it is not working.
Someone help me out.
delimiter //
CREATE PROCEDURE `classicWatches`.kill_long_running_queries()
BEGIN
DECLARE process_id BIGINT;
DECLARE finished INT DEFAULT 0;
DECLARE kill_process_id CURSOR FOR SELECT ID FROM INFORMATION_SCHEMA.PROCESSLIST WHERE COMMAND = 'Query' AND TIME > 5; # only for 5 seconds just testing whether it works or not
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
OPEN kill_process_query;
loop_loop: LOOP
FETCH kill_process_id INTO process_id;
IF process_id >=1 THEN
KILL QUERY process_id ; # Here showing error in mysql workbench but don't know what error
END IF ;
IF finished THEN
LEAVE loop_loop;
END IF;
END LOOP loop_loop;
CLOSE kill_process_id;
END //
delimiter ;
CREATE EVENT kill_long_running_queries
ON SCHEDULE EVERY 10 SECOND
DO CALL `classicWatches`.kill_long_running_queries();
I'm not quite sure what you want to accomplish.
The error shown in Workbench is because an integer is expected (as shown on line 23 - line 23 is just an example), but it is found process_id variable, however, the stored procedure is created and executed correctly when invoked.
The cursor is kill_process_id, but on line 13 you open the cursor kill_process_query does not exist, you need to change for line 14.

Transferring MSSQL Stored Procedures to MySQL

I have an MSSQL stored procedure as follows :-
CREATE PROCEDURE [dbo].[user_CheckEMail]
#Email nvarchar(200)
AS
BEGIN
IF EXISTS (SELECT * FROM user_Data WHERE EMail = #Email)
BEGIN
RETURN 1
END
ELSE
BEGIN
RETURN 0
END
END
In changing to MySQL I know that certain things need to change. This is what I have :-
CREATE PROCEDURE server.`user_CheckEMail`(
IN Email nvarchar(200))
BEGIN
DECLARE this_count int;
SET this_count = (SELECT COUNT(*) FROM user_Data WHERE EMail = Email)
IF (this_count > 0) THEN
RETURN 1
ELSE
RETURN 0
END IF
END
In this state "int" is highlighted with "syntax error, unexpected END_OF_INPUT, expecting ';'"
By removing the ";" the Declare line loses it's error, but the Set line now shows "syntax error, unexpected SET, expecting ';'"
If I remove the Declare statement altogether the Set line is fine and the If line returns "syntax error, unexpected IF, expecting ';'"
Finally, by placing ';' at the end of the Set statement the Set returns error "syntax error, unexpected END_OF_INPUT, expecting ';'" while the If now errors "syntax error, unexpected IF"
I know that this is only a basic procedure, but I have about two dozen to move across and all are showing errors, so I'm hoping find out what I'm doing wrong here and I'll be able to cure all problems.....hopefully.
Try this-
DELIMITER $$
CREATE PROCEDURE server.`user_CheckEMail`(
IN Email nvarchar(200))
BEGIN
DECLARE this_count int;
SET this_count = (SELECT COUNT(*) FROM user_Data WHERE EMail = Email);
IF (this_count > 0) THEN
RETURN 1
ELSE
RETURN 0
END IF;
END$$
By default MySQL treats semicolon(;) as the statement terminator or end of statement. But as we need to use it inside the procedure body, so we need another different delimiter to state the end of the stored procedure. Here DELIMITER $$ sets $$ as the statement terminator.

How do i get column values of my sql table dynamically using stored procedures?

I am learning stored procedures. Coming to the problem I have a table where columns are named as Q1,Q2,Q3...,Q10 . I want to retrieve these values dynamically using while loop (or for loop). I tried something but that didn't give me actual output. Please give me some suggestions.
My code is as follows :
delimiter //
drop procedure if exists exam//
create procedure exam ()
begin
declare a int default 1;
while a <= 9 do
select concat(Q,a) from feed1 where col = 'XYZ';
set a= a + 1;
end while;
end ;
//
delimiter ;
call exam;
When i run this i am getting an error " Unknown column Q in field list "

mysql stored function - in varchar and return varchar - Error : 1172

I could not figure out what went wrong with my stored function.
select batch as bach from test where mfg_code = 'BC-7A1-5' group by batch;
When I run the script above and I am able to get the number of rows as expected.
However I am not able with the similar script below:-
DELIMITER $$
DROP FUNCTION IF EXISTS `testdata1970_05`.`listbatch` $$
CREATE FUNCTION `listbatch`(mfgnum VARCHAR(24)) RETURNS VARCHAR(10)
BEGIN
DECLARE bach VARCHAR(10);
SELECT batch into bach FROM test WHERE mfg_code = mfgnum group by batch;
RETURN bach;
END $$
DELIMITER ;
And it returns error:
"ERROR 1172 (42000): Result consisted of more than one row"
And here is my query below:
select listbatch("BC-7A1-5");
Your error message is telling you that more than one row is being returned by your query, and the resultset cannot be stored in a simple variable, as the variable you defined can hold only one value!