mysql truncating error - mysql

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

Related

Create mysql trigger before insert (Syntax error)

Hi i am creating some trigger for update the value in insert query before insert the row like this
CREATE TRIGGER `Insert members in Posts` BEFORE INSERT ON
`posts` FOR EACH ROW
BEGIN
DECLARE name_ varchar(100);
DECLARE contact_ varchar(16);
DECLARE status_ int;
DECLARE deleted_ int;
SELECT
`members`.`name`,
`members`.`contact`,
`members`.`status`,
`members`.`deleted`
INTO name_, contact_, status_, deleted_
FROM
`members`
WHERE
`members`.`member_id` = NEW.member_id;
SET NEW.member_name = name_;
SET NEW.member_contact = contact_;
SET NEW.member_status = status_;
SET NEW.member_deleted = deleted;
END
But i received the error like
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
I am little confused what's the error in line no 4. Please explain someone. Thanks.
Depending on the client/ide used you may need to set delimiter for example
drop trigger if exists t;
delimiter $$
CREATE TRIGGER t BEFORE INSERT ON
`posts` FOR EACH ROW
BEGIN
DECLARE name_ varchar(100);
DECLARE contact_ varchar(16);
DECLARE status_ int;
DECLARE deleted_ int;
SELECT
`members`.`name`,
`members`.`contact`,
`members`.`status`,
`members`.`deleted`
INTO name_, contact_, status_, deleted_
FROM
`members`
WHERE
`members`.`member_id` = NEW.member_id;
SET NEW.member_name = name_;
SET NEW.member_contact = contact_;
SET NEW.member_status = status_;
SET NEW.member_deleted = deleted;
END $$
delimiter ;
please review https://dev.mysql.com/doc/refman/8.0/en/stored-routines.html

Mysql function - cant figure it out

i'm using MySql Workbench and im unable to figure out this.
delimiter $$
CREATE function `klientu_copy`()
DECLARE v_laiks TIMESTAMP;
DECLARE v_liet VARCHAR(200);
set v_laiks = now();
set v_liet = current_user;
if (TG_OP = 'DELETE') THEN
insert into kopija_klienti values (v_liet,v_laiks,old.Vards,old.Uzvards,null,null);
ELSEif (TG_OP = 'INSERT') THEN
insert into kopija_klienti values (v_liet,v_laiks,null,null,new.Vards,new.Uzvards);
ELSEif (TG_OP='UPDATE') THEN
insert into kopija_klienti values (v_liet,v_laiks,old.Vards,old.Uzvards,new.Vards,new.Uzvards);
end if;
END; $$
delimiter ;
21:24:22 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 ''klientu_copy' () BEGIN DECLARE v_laiks timestamp; DECLARE v_liet varchar; s' at line 1 0.000 sec
Did try set #variable / declare #variable, cant figure this out. I'm still learning :)
You must specify the length of the varchar variable, like varchar(100)
Also add the closing ( for the first insert.

How to debug this MySQL stored procedure syntax error?

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 ...

mySQL Trigger on second table

Please help with this MySQL trigger - have searched and can not find problem with syntax.
Getting 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 '#v_index int DEFAULT 1; WHILE #v_index <= NEW.pt_number INSERT INTO Pat' at line 9
DELIMITER $$
CREATE TRIGGER after_insert_calls_insert_patients
AFTER INSERT ON Calls
FOR EACH ROW
BEGIN
DECLARE #v_index int DEFAULT 1;
WHILE #v_index <= NEW.pt_number
INSERT INTO Patients SET
Patient_id = CONCAT(NEW.Calls_id, '-', #v_index),
FK_Calls_id = NEW.Calls_id,
update_by = "SYSTEM";
SET #v_index := #v_index +1;
END WHILE;
END;
$$
DELIMITER ;
Try dropping the # in "DECLARE #v_index int DEFAULT 1;"

Error with MySQL, DECLARE

I am using MySQL v5.0.92 and I'm trying to import some data, but I get an error on declare my variables.
BEGIN
DECLARE flag INT;
DECLARE id INT;
while flag=0 begin
SET id=(SELECT top 1 user_id
FROM ac_user_info WHERE user_id>#id order by user_id)
INSERT INTO cuddleew_database1.cewp_usermeta(user_id,meta_key,meta_value)
SELECT id,'first_name',first_name
FROM cuddleew_backup.ac_user_info WHERE user_id=#id
INSERT INTO cuddleew_database1.cewp_usermeta(user_id,meta_key,meta_value)
SELECT id,'last_name',last_name
FROM cuddleew_backup.ac_user_info WHERE user_id=#id
SET flag=(select case #id when(SELECT MAX(user_id)
FROM cuddleew_bakup.ac_user_info) then 1
else 0
END CASE)
END WHILE
END
In MySQL console i get this:
BEGIN DECLARE flag INT;
#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 flag INT' at line 2.
Can you help me with this and tell me what's wrong. Thanks in advance.
You have to change your delimiter first, so the ; doesn't tell MySQL that this command is over.
DELIMITER $$
[your code here]
END $$
DELIMITER ;