My procedure gives error when I run. I couldn't find where is mistake ?
CREATE PROCEDURE testing ()
BEGIN
DECLARE i INT;
DECLARE vSite VARCHAR(100);
set #i = 1;
BEGIN WHILE #i <= 5 DO
SET #vSite = #vSite + CONCAT('LINE '+#i+', ');
SET #i = #i + 1;
END WHILE
SELECT #vSite;
END
/* Error
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 'SELECT #vSite; END' at line 10
*/
/* required output given below
LINE 1, LINE 2, LINE 3, LINE 4, LINE 5,
*/
Resolved By My Self
delimiter //
CREATE procedure while_examples()
wholeblock:BEGIN
declare str VARCHAR(255) default '';
declare x INT default 0;
SET x = 1;
WHILE x <= 5 DO
SET str = CONCAT(str,'LINE',x,',');
SET x = x + 1;
END WHILE;
select str;
END//
CALL while_examples();
Related
What is the correct way to generate a new id as varchar? Here I'm trying to convert from SQL Server to MySQL database.
Help needed.
I tried internet but it did not solve my problem.
--*GENERATES ANY ID WHEN NEED*
DELIMITER //
CREATE FUNCTION getNewID(needTable VARCHAR(20)) RETURNS VARCHAR(10)
BEGIN
DECLARE lastvalue VARCHAR(10);
DECLARE i INT;
DECLARE newId VARCHAR(10);
IF needTable = 'Item'
SELECT lastvalue = MAX(resourceID) FROM Item;
SELECT MAX(resourceID) INTO lastvalue FROM Item;
IF IS NULL(lastvalue)
SET lastvalue = 'I00000000';
SET i = RIGHT(lastvalue,9) + 1;
SET newId = 'S' + RIGHT('00000000'+CONVERT(VARCHAR(10),i),9);
RETURN newId;
END; //
DELIMITER ;
SELECT getNewID ('Item');
DROP FUNCTION getNewID
The error says:
Error code 1064, SQL state 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 'SELECT MAX(resourceID) INTO lastvalue FROM Item;
IF IS NULL(lastval' at line 10
Line 3, column 1
Execution finished after 0 s, 1 error(s) occurred.
Try this
DELIMITER //
CREATE FUNCTION getNewID(needTable VARCHAR(20)) RETURNS VARCHAR(10)
BEGIN
DECLARE lastvalue, newId VARCHAR(10);
DECLARE i INT;
SELECT MAX(resourceID) INTO lastvalue FROM Item where needTable = 'Item';
IF(lastvalue IS NULL) THEN
SET lastvalue = 'I00000000';
SET i = RIGHT(lastvalue,9) + 1;
SET newId = 'S' + RIGHT('00000000'+CONVERT(VARCHAR(10),i),9); -- what you are trying to do here?
END IF;
-- what you need to return whenlastvalue is not null?
RETURN newId;
END //
DELIMITER ;
I got a hint from O.jones and went to create a stored function.
That's what I created:
DELIMITER $$
CREATE FUNCTION change_int(colres VARCHAR(500)) RETURNS INT(11)
BEGIN
DECLARE res int(11);
DECLARE leng int(11);
DECLARE newres int(11);
DECLARE mult int(11);
DECLARE temp1 int(11);
DECLARE temp2 int(11);
SET res = CAST(colres AS UNSIGNED);
SET leng = CHAR_LENGTH(CAST( colres AS CHAR));
SET newres = 0;
SET mult = 1;
SET temp1 = 0;
SET temp2 = 0;
WHILE (res > 0) DO
SET temp1 = MOD(res , 10 );
SET res = (res DIV 10);
SET temp2 = MOD(res , 10 );
SET newres = (newres +((temp1 + temp2 ) * mult));
SET mult = mult*10;
END WHILE;
SET newres = SUBSTRING (newres, 1, leng );
RETURN newres;
END $$
DELIMITER ;
But I get error when I ty to run it on line 6 :
#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 ''res' = CAST(colres AS CHAR)' at line 6
Added a delimiter, new 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 'SET newres = SUBSTRING (newres, 1, leng );
RETURN newres;
END' at line 27
Greate now it worked.
But I can not evoke her:
Ran a test with:
SHOW FUNCTION STATUS;
And it exists.
When I try to ran it like this:
UPDATE `table` SET `col1` = function_name(`col1`);
Also tried:
UPDATE `table` SET `col1` = db.function_name(`col1`);
No luck.
You have missing column before END WHILE. It must be END WHILE;
Working example
I am trying to:
1. Print all the values in between #loop_start_date to #loop_end_date
2. loop_start_date should be increased by 1 here till loop_start_date=loop_end_date
I tried below but Mysql says
#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 'BEGIN Declare #user_id int' at line 3
Here is the procedure code
CREATE PROCEDURE generatePayscale
BEGIN
Declare #user_id int;
Declare #office_id int;
Declare #loop_start_date Date;
Declare #loop_end_date Date;
SET #user_id = 1287;
SET #office_id = 8;
SET #loop_start_date = '2018-06-02';
SET #loop_end_date = '2018-06-06';
WHILE(#loop_start_date < #loop_end_date) do
SELECT old_paysetupid
FROM personneltransfer personnelid=#user_id
AND pt.transferdate>#loop_start_date
END WHILE;
END
This may point you in the correct direction, a few things i would change. Added BEGIN. Then added declare for each of the vars. Then you need a where after the from
SET #user_id = 1287;
SET #office_id = 8;
SET #loop_start_date = '2018-06-02';
SET #loop_end_date = '2018-06-06';
WHILE(#loop_start_date < #loop_end_date) do
BEGIN
SELECT old_paysetupid
FROM personneltransfer where personnelid=#user_id
and pt.transferdate>#loop_start_date
END WHILE;
begin
declare currentMonth int;
declare half int default 0;
set currentMonth = (select DATE_FORMAT(now(),"%m"));
if(currentMonth > 5)
**begin**
set half = 1
end
end;
This is screen shot of what error is shown
Showing me this error: MySQL server version for the right syntax to use near 'begin set half = 1; end select currentMonth into half' at line 6
Use into in select set is not uset
begin
declare currentMonth int;
declare half int default 0;
select DATE_FORMAT(now(),"%m") INTO #currentMonth;
if(currentMonth > 5) THEN
set half = 1;
end if;
end
I am having a simple nested while loop in stored procedure , but it's giving an error.
The loop does not nothing great, just for learning purpose i created two loops.
delimiter $$
create procedure getSum(in input int , out output int)
begin
set output = 0;
while input >= 1 do
declare tmp int default 1;
while tmp <= 5 do
set output = output + input ;
set tmp = tmp + 1;
end while ;
set input = input - 1 ;
end while;
end $$
delimiter ;
Below 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 'declare tmp int default 1; while tmp <= 5 do set output = output + inpu' at line 7
Thanks.
Try this:
delimiter $$
create procedure getSum(in input int , out output int)
begin
declare tmp int default 1;
set output = 0;
while input >= 1 do
set tmp = 1;
while tmp <= 5 do
set output = output + input ;
set tmp = tmp + 1;
end while ;
set input = input - 1 ;
end while;
end $$
delimiter ;