I have written a stored procedure in MySQL. I am getting issue in casting a day value from a date into integer value. I need to cast Day(now()) into integer, as I need to do some manipulation on this value.
Please see below for detail procedure code.
DROP PROCEDURE IF EXISTS `CREATE_PARTITION_FOR_MONTH`();
delimiter //
CREATE PROCEDURE `CREATE_PARTITION_FOR_MONTH`()
BEGIN
DECLARE current_month_total_days_count INT;
DECLARE current_month_days_left INT;
DECLARE current_day INT;
DECLARE next_month_first_date DATE ;
DECLARE loop_start_val INT DEFAULT 0;
DECLARE total_days_in_next_month INT;
DECLARE next_month_var_date DATE;
DECLARE partition_val VARCHAR(20) DEFAULT NULL;
DECLARE range_val INT DEFAULT 0;
SET current_month_total_days_count := CAST(DAY(LAST_DAY(now())) AS UNSIGNED);
SET current_day := CAST(EXTRACT(MONTH FROM CURDATE()) AS UNSIGNED);
SET current_month_days_left := current_month_total_days_count - current_day;
SET next_month_first_date := DATE(ADDDATE(CURDATE(),current_month_days_left));
SET total_days_in_next_month := DAY(LAST_DAY(next_month_first_date));
SET next_month_var_date := next_month_first_date;
SET loop_start_val := 0;
WHILE loop_start_val < total_days_in_next_month DO
SET next_month_var_date := DATE(ADDDATE(next_month_first_date,loop_start_val));
SET partition_val := CONCAT('p',TO_DAYS(next_month_var_date));
SET range_val := TO_DAYS(next_month_var_date);
ALTER TABLE XYZ ADD PARTITION (PARTITION partition_val VALUES LESS THAN (range_val));
SET loop_start_val := loop_start_val+1;
END WHILE;
END //;
DELIMITER ;
I am getting this 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 'range_val INT DEFAULT 0;
SET current_month_total_days_count = SELECT CAST(DAY(L' at line 11
Look at the line throwing error *DELCARE* range_val INT DEFAULT 0; ... DELCARE is the issue. Should be DECLARE. Funny ...
Related
I am trying to create and set a variable:
DECLARE myId INT;
SET myId = 5;
However, I am getting invalid syntax complaint in MySQL Workbench:
SQL syntax error near 'DECLARE myId INT;'
I have tried the following variants:
DECLARE myId INT(4);
SET myId = 5;
DECLARE #myId INT;
SET #myId = 5;
DECLARE #myId INT(4);
SET #myId = 5;
What is wrong?
Try
SET #myId := 100;
Then if you do
select #myId;
You will get
100
As in the comment says Declare is only valid into stored programs like procedures, functions.
here you have an example of a store procedure and its call.
DELIMITER $$
CREATE PROCEDURE sp1 (x VARCHAR(5))
BEGIN
DECLARE xname VARCHAR(5) DEFAULT 'bob';
DECLARE myId INT;
SET myId = 5;
SELECT CONCAT(xname,' -- ',myId);
END;
$$
DELIMITER ;
call sp1('MY NAME');
I experienced the same problem. The variables must be declared at the beginning of the script.
DELIMITER &&
DROP PROCEDURE IF EXISTS PS_HANDLERS;
CREATE PROCEDURE PS_HANDLERS(IN ID_USER INT, OUT isError INT)
BEGIN
DECLARE USER_EMAIL VARCHAR(50);
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
BEGIN
SET IsError = 1;
END;
SET USER_EMAIL = CONCAT(RAND(),'#',RAND(),'.com');
SET isError = 0;
INSERT INTO tbl_user VALUES(ID_USER, 'ipsum','lorem','ipsum#lorem.com','password','ROLE_USER');
SELECT
u.*
FROM
tbl_user u;
END &&
DELIMITER ;
I am getting a syntax error on executing the below function in MySQL.
CREATE FUNCTION FABC (P_MEMBER_EMAIL VARCHAR(30))
RETURNS int
BEGIN
DECLARE RESULT int;
DECLARE V_DATE DATE;
SELECT DOJ+365 INTO V_DATE FROM CHYK_MEMBER_MASTER
WHERE UPPER(MEMBER_EMAIL)=UPPER(P_MEMBER_EMAIL) AND SUBSCRIPTION='Y';
IF V_DATE>SYSDATE THEN
SET RESULT=1;
ELSE
SET RESULT=0;
END IF;
RETURN RESULT;
END;
The error is as follows "#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 4
Not sure what's causing this. Can anyone help me ?
You need to set a delimiter that will tell MySQL the END of the function definition.
DELIMITER $$
CREATE FUNCTION `FABC`(`P_MEMBER_EMAIL` VARCHAR(30)) RETURNS int(11)
BEGIN
DECLARE RESULT int;
DECLARE V_DATE DATE;
SELECT DOJ+365 INTO V_DATE FROM CHYK_MEMBER_MASTER
WHERE UPPER(MEMBER_EMAIL)=UPPER(P_MEMBER_EMAIL) AND SUBSCRIPTION='Y';
IF V_DATE>SYSDATE THEN
SET RESULT=1;
ELSE
SET RESULT=0;
END IF;
RETURN RESULT;
END$$
DELIMITER ;
I've written a function but it gives me mistake a the second line (create statement) if anyone could help me, I really appreciate:
CREATE FUNCTION GetPrefix (phone_num VARCHAR(30)) RETURNS varchar(30)
deterministic
BEGIN
DECLARE x INT;
DECLARE prefix varchar(30);
SET x = 0;
for prefix in SELECT code
FROM tab_len
while (length(phone_num)) > 0
do
if prefix<>left(phone_num, length(phone_num)-x)
then set x=x+1 ;
else return 1 ;
END while ;
END $$;
and I receive this error :
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 'for prefix in SELECT code
FROM tab_len while (length(phone_n' at line 9
DELIMITER $$
DROP FUNCTION IF EXISTS GetPrefix $$
CREATE FUNCTION GetPrefix
(
phone_num VARCHAR(30)
)
RETURNS varchar(30)
BEGIN
DECLARE var_x INT DEFAULT 0;
DECLARE var_prefix VARCHAR(100);
SET phone_num = IFNULL(phone_num,'');
-- your logic will go here.
return phone_num;
END$$
DELIMITER ;
SELECT GetPrefix('test');
This is right syntax to write a function in mysql.
check out the differences. Take a look Here
What is wrong in the syntax of the below stored procedure in MySQL ?
CREATE PROCEDURE curdemo()
BEGIN
DECLARE #isbn varchar(17)
DECLARE #count int
DECLARE #price float(6,2)
DECLARE #totalbookpriceValue float(6,2)
DECLARE totalbookprice CURSOR
STATIC FOR
(SELECT ISBN,COUNT(ISBN) FROM applieddb.cart c GROUP BY ISBN)
OPEN totalbookprice
IF ##CURSOR_ROWS > 0 BEGIN
FETCH NEXT FROM cur_emp INTO #isbn,#count
WHILE ##Fetch_status = 0
SELECT ISBN,PRICE FROM book INTO #price WHERE ISBN = #isbn
PRINT 'Total Book Price is' + #price * #count
FETCH NEXT FROM cur_emp INTO #isbn,#count
END
END
CLOSE totalbookprice
DEALLOCATE totalbookprice
SET NOCOUNT OFF
END
ERROR Message:
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 '#isbn varchar(17) DECLARE #count int DECLARE #price float(6,2) DECLARE #totalboo' at line 3
Changing the mysql query to the below also did not help and gave the following error:
DECLARE for local (without #) or SET for session (with #)
***The solution to the above problem is as below:***
CREATE DEFINER=`root`#`localhost` PROCEDURE `calculateTotalPrice`()
BEGIN
DECLARE done INT default 0;
DECLARE _isbn varchar(17);
DECLARE _count int;
DECLARE _price float(6,2);
DECLARE loop_cntr INT DEFAULT 0;
DECLARE _totalbookpriceValue float(6,2);
DECLARE totalbookprice CURSOR FOR SELECT ISBN,COUNT(ISBN) FROM applieddb.cart c GROUP BY ISBN;
DECLARE CONTINUE HANDLER FOR NOT FOUND set done= TRUE;
OPEN totalbookprice;
the_loop: LOOP
FETCH totalbookprice into _isbn,_count;
SET _price= (SELECT b.price from book b where b.ISBN = _isbn);
SET _totalbookpriceValue= _price*_count;
SELECT _totalbookpriceValue;
IF done THEN
CLOSE totalbookprice;
LEAVE the_loop;
END IF;
-- count the number of times looped
SET loop_cntr = loop_cntr + 1;
END LOOP the_loop;
END
Create procedure a06_generate_data( p_loop_count int,
p_min int,
p_max int,
p_status char(1))
begin
declare v_max int;
declare v_min int;
declare v_loop_count int;
set v_max := p_max;
set v_min := p_min;
set v_loop_count := p_loop_count
-- clear the results table for each run
truncate table p_testbed.a06_rndData;
Repeat
insert into p_testbed.a06_rndData (col_value)
values (floor(v_min + rand()*(v_max – v_min)));
set v_loop_count := v_loop_count – 1;
until v_loop_count <= 0 End Repeat;
select col_value p_testbed.a06_rndData ;
end; #
This procedure is to insert random values starting from p_min to p_max with a total of p_loop_count values. But the problem is, there's an error around truncate:
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 'truncate table p_testbed.a06_rndData;
Repeat
insert into p_testbed.a0'
I've searched online to check for the syntax for truncate, but I think I have this written correctly.
Suggestions? Thanks.
The problem is not with TRUNCATE TABLE, the line before is missing a semicolon:
set v_loop_count := p_loop_count