I am testing out my trigger and it works well all except i can't seem my variables don't seem to work like they should.
For instance
DELIMITER //
CREATE TRIGGER lestrigger
AFTER INSERT ON examinations
FOR EACH ROW
BEGIN
DECLARE the_last_inserted_id INT ;
DECLARE the_class_id INT;
DECLARE the_year_id INT;
SET the_last_inserted_id = LAST_INSERT_ID();
SET the_class_id = (select examination_class_id from examinations where examination_id = 1);
SET the_year_id = (select examination_class_id from examinations where examination_id = 1);
insert into examination_data (ed_cs_id,ed_examination_id) select cs_id,#the_last_insert_id from class_students where cs_class_id = 1 AND cs_year_id = 1;
END //
DELIMITER ;
In this line
insert into examination_data (ed_cs_id,ed_examination_id) select cs_id,the_last_insert_id from class_students where cs_class_id = 1 AND cs_year_id = 1;
the_last_insert_id is being seen as a column
When i try this,#the_last_insert_id,its not seen as a column but its not working either.I have not tried the rest of my variables.
How am i going to define and use this the_last_inserted_id = LAST_INSERT_ID(); for instance?.
You can set
Select STH into the_last_inserted_id from TBL limit 1;
insert into examination_data (ed_cs_id,ed_examination_id)
select cs_id,the_last_insert_id
from class_students
where cs_class_id = 1 AND cs_year_id = 1;
Edit:
# - You are using without declaring, just seting
Select STH into #new_id from TBL limit 1;
and then
insert into examination_data (ed_cs_id,ed_examination_id)
select cs_id,#the_last_insert_id
from class_students
where cs_class_id = 1 AND cs_year_id = 1;
Related
On MySql 5.7, I'd like to run this procedure which is supposed to fetch tag1 of articles from article table and insert them into article_tag table:
DELIMITER $$
CREATE PROCEDURE dt1()
BEGIN
DECLARE maxid INT;
DECLARE x INT;
DECLARE tag1 VARCHAR(30);
DECLARE ntag1 int;
SET maxid = (SELECT MAX(id) FROM `article`);
SET x = (SELECT MIN(id) FROM ` article`) ;
WHILE x<= maxid DO
SET tag1 = (SELECT tag1 from `article` WHERE id=x);
call debug_msg(TRUE, tag1);
SET ntag1 = (SELECT count(*) from `article_tag` WHERE tag=tag1);
IF ntag1 = 0 THEN INSERT INTO `article_tag` (tag, slug, frequency) VALUES (tag1, tag1, 1);
ELSE UPDATE ` article_tag` SET frequency = frequency + 1 WHERE tag=tag1 ;
END IF;
SET x = x + 1;
END WHILE;
END$$
I can confirm than none of the tag1 values is null:
SELECT count(*) from `article` WHERE tag1 IS NULL;
+----------+
| count(*) |
+----------+
| 0 |
+----------+
However when I run the procedure, I get:
ERROR 1048 (23000): Column 'tag' cannot be null
Here is a reproducible example.
https://www.db-fiddle.com/f/3ymPtabSksRUMSBep7SyX8/5
This problem bugs me for hours. So appreciate on hints on why this happens, and how to fix it?
I'm making a stored routine in MySQL, and I would like to know whether it is possible to make an INSERT-IF some condition is TRUE, ELSE INSERT something else.
This is My Query but I keep getting the #1064 Error (SQL Syntax Error).
DECLARE a INT;
DECLARE b INT;
DECLARE changeTypeID INT DEFAULT(0);
SET a = (SELECT roomPrice FROM RPH WHERE tier=1 AND
startDate = '2018-02-20' AND rTypeID=1);
SET b = (SELECT roomPrice FROM RPH WHERE tier=1 AND
startDate = '2018-02-20' AND rTypeID=2);
SET changeTypeID = CASE WHEN (a>b) THEN 1 WHEN (a<b) THEN 2 WHEN a=b THEN 3 END;
IF (changeTypeID = 1 OR changeTypeID =2) THEN
(
INSERT INTO TABLE_A (X,Y,Z) VALUES (ID, changeTypeID, createdBy);
)
ELSEIF (changeTypeID = 3) THEN
(
INSERT INTO TABLE_B (P,Q,R) VALUES (ID, rID, createdBy);
)
END IF;
Here is the error message:
SQL Error (1064): You have an error in your SQL Syntax; check the manual
that corresponds to your MySQL version for the right syntax to use near
'INSERT INTO TABLE_A (X,Y,Z)' at line 19
Note: The insert parameter is already declared on the routine beforehand.
Any help will be appreciated :)
DECLARE a INT;
DECLARE b INT;
DECLARE changeTypeID INT DEFAULT(0);
SET a = (SELECT roomPrice FROM RPH WHERE tier=1 AND
startDate = '2018-02-20' AND rTypeID=1);
SET b = (SELECT roomPrice FROM RPH WHERE tier=1 AND
startDate = '2018-02-20' AND rTypeID=2);
SET changeTypeID = CASE WHEN (a>b) THEN 1 WHEN (a<b) THEN 2 WHEN a=b THEN 3 END;
IF (changeTypeID = 1 OR changeTypeID =2) THEN
INSERT INTO TABLE_A (X,Y,Z) VALUES (ID, changeTypeID, createdBy);
ELSEIF (changeTypeID = 3) THEN
INSERT INTO TABLE_B (P,Q,R) VALUES (ID, rID, createdBy);
END IF;
SELECT COUNT(id) INTO student_count FROM student_info WHERE roll_no=roll_no and division_id = division_id and institute_id=institute_id and academic_year_id = academic_year;
I am getting student_count = 1
when I am trying to execute stored procedure.
When I am executing direct query as below:
SELECT COUNT(*) AS c FROM student_info WHERE division_id=9 AND institute_id=1 and academic_year_id =11 and roll_no='12617690';
I am getting result 0 which is right. My procedure is:
**CREATE DEFINER = 'root'#'localhost'
PROCEDURE smart_school.create_student(IN academic_year int(11),IN standard_id int(11),IN division_id int(11),IN student_user_id int(11),IN gr_no VARCHAR(20),IN roll_no VARCHAR(50),IN institute_id INT(11),IN maker_id INT(11),OUT create_student_result varchar(20))
BEGIN
DECLARE student_id int;
DECLARE student_count int;
DECLARE semester varchar(10);
DECLARE exit handler for sqlexception
BEGIN
SET create_student_result = "failure";
SELECT #create_student_result;
ROLLBACK;
END;
IF NOT EXISTS (select id from role_user where user_id = student_user_id and role_id = 3 and institute_id=institute_id limit 0,1) THEN
BEGIN
select COUNT(id) INTO student_count from student_info WHERE roll_no=roll_no and division_id = division_id and institute_id=institute_id and academic_year_id = academic_year;
if student_count < 1 then
BEGIN
START TRANSACTION;
insert into student (is_active,user_id,maker_id,caste_id,category_id,created_at,updated_at) values ('1',student_user_id,maker_id,1,1,NOW(),NOW());
SET student_id = LAST_INSERT_ID();
select semester_id into semester from division where institute_id=institute_id and standard_id=standard_id limit 0,1;
insert into student_info (roll_no,is_active,institute_id,student_id,standard_id,semester_id,division_id,academic_year_id,maker_id,created_at,updated_at) values (roll_no,'1',institute_id,student_id,standard_id,semester,division_id,academic_year,maker_id,now(),now());
insert into institute_student (is_active,gr_no,institute_id,student_id,admission_standard_id,admission_year_id,created_at,updated_at) values ('1',gr_no,institute_id,student_id,standard_id,academic_year,now(),now());
insert into role_user (user_id,role_id,institute_id,is_active,joining_date,created_at) values (student_user_id,3,institute_id,'1',now(),now());
COMMIT;
SET create_student_result = "success";
SELECT #create_student_result;
END;
ELSE
BEGIN
SET create_student_result = "duplicate_rollno";
SELECT #create_student_result;
END;
END IF;
END;
ELSE
BEGIN
SET create_student_result = "already_exists";
SELECT #create_student_result;
END;
END IF;
END**
i want to enhance performance of following Mysql Stored Procedure.
i am trying to XML with 100 items. and it is Taking almost 90 seconds. and Its too much.
Please check my following SP.
DELIMITER $$
DROP PROCEDURE IF EXISTS `kshitij`.`Insert_date` $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `Insert_date`()
BEGIN
DECLARE itemstart INTEGER default 1;
DECLARE valuestart INTEGER default 1;
DECLARE itemcount INTEGER;
DECLARE valuecount INTEGER;
SET #xml = '<items>
<size>2</size>
<item>
<value columntype="0" columnid="23">Single Line Text_0</value>
<value columntype="1" columnid="24">Multi Line Text_0</value>
<value columntype="2" columnid="25">Number_0</value>
<value columntype="3" columnid="26">Link_0</value>
<value columntype="4" columnid="27">Image_0</value>
<value columntype="5" columnid="28">Date time_0</value>
</item>
TRYING WITH 100 ITEMS.
</items>';
SET itemcount = (SELECT ExtractValue(#xml, 'count(//item)'));
WHILE itemstart < itemcount
DO
SET valuecount = (SELECT ExtractValue(#xml, 'count(/items/item[itemstart]/value)'));
SET valuestart = 1;
WHILE valuestart < valuecount
DO
IF((SELECT ExtractValue(#xml, '/items/item[$itemstart]/value[$valuestart]/#columntype')) = '0') THEN
INSERT INTO singleline_text(value) VALUES(ExtractValue(#xml, '/items/item[$itemstart]/value[$valuestart]'));
INSERT INTO sheet_items(c_id, value_id) VALUES(ExtractValue(#xml, '/items/item[$itemstart]/value[$valuestart]/#columnid'), (SELECT MAX(id) FROM singleline_text));
END IF;
IF((SELECT ExtractValue(#xml, '/items/item[$itemstart]/value[$valuestart]/#columntype')) = '1') THEN
INSERT INTO multiline_text(value) VALUES(ExtractValue(#xml, '/items/item[$itemstart]/value[$valuestart]'));
INSERT INTO sheet_items(c_id, value_id) VALUES(ExtractValue(#xml, '/items/item[$itemstart]/value[$valuestart]/#columnid'), (SELECT MAX(id) FROM multiline_text));
END IF;
IF((SELECT ExtractValue(#xml, '/items/item[$itemstart]/value[$valuestart]/#columntype')) = '2') THEN
INSERT INTO number(value) VALUES(ExtractValue(#xml, '/items/item[$itemstart]/value[$valuestart]'));
INSERT INTO sheet_items(c_id, value_id) VALUES(ExtractValue(#xml, '/items/item[$itemstart]/value[$valuestart]/#columnid'), (SELECT MAX(id) FROM number));
END IF;
IF((SELECT ExtractValue(#xml, '/items/item[$itemstart]/value[$valuestart]/#columntype')) = '3') THEN
INSERT INTO link(value) VALUES(ExtractValue(#xml, '/items/item[$itemstart]/value[$valuestart]'));
INSERT INTO sheet_items(c_id, value_id) VALUES(ExtractValue(#xml, '/items/item[$itemstart]/value[$valuestart]/#columnid'), (SELECT MAX(id) FROM link));
END IF;
IF((SELECT ExtractValue(#xml, '/items/item[$itemstart]/value[$valuestart]/#columntype')) = '4') THEN
INSERT INTO image(value) VALUES(ExtractValue(#xml, '/items/item[$itemstart]/value[$valuestart]'));
INSERT INTO sheet_items(c_id, value_id) VALUES(ExtractValue(#xml, '/items/item[$itemstart]/value[$valuestart]/#columnid'), (SELECT MAX(id) FROM image));
END IF;
IF((SELECT ExtractValue(#xml, '/items/item[$itemstart]/value[$valuestart]/#columntype')) = '5') THEN
INSERT INTO date_time(value) VALUES(ExtractValue(#xml, '/items/item[$itemstart]/value[$valuestart]'));
INSERT INTO sheet_items(c_id, value_id) VALUES(ExtractValue(#xml, '/items/item[$itemstart]/value[$valuestart]/#columnid'), (SELECT MAX(id) FROM date_time));
END IF;
SET valuestart = valuestart + 1;
END WHILE;
SET itemstart = itemstart + 1;
end while;
END $$
DELIMITER ;
Thanks.
When i execute this trigger it throws error as
#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 'END' at line 27
delimiter //
CREATE TRIGGER `after_tblgps_insert` AFTER INSERT ON `tbl_gps`
FOR EACH ROW BEGIN
DECLARE cab_meter_new VARCHAR(10);
DECLARE cab_cost_new VARCHAR(10);
DECLARE cab_meter_old VARCHAR(10);
DECLARE cab_cost_old VARCHAR(10);
DECLARE pr_pnr_no VARCHAR(15);
DECLARE pr_dest_id VARCHAR(15);
select NEW.gps_rs232,NEW.gps_cab_bus,gps_rs232,gps_cab_bus into #cab_meter_new,#cab_cost_new,#cab_meter_old,#cab_cost_old from tbl_gps where gps_imei = NEW.gps_imei order by gps_id asc limit 0,1;
IF #cab_meter_new = 1 and #cab_meter_old = 0 THEN
BEGIN
select `booking_pnr_no` into #pr_pnr_no from tbl_booking where booking_alloted_vehicle = (select taxi_id from tbl_taxi where taxi_imei = NEW.gps_imei ) AND booking_status = 13;
select trip_dst_area_id into #pr_dest_no from tbl_trip where pnr_id = #pr_pnr_no order by trip_id desc limit 0,1;
update tbl_taxi set taxi_cur_status = 12,taxi_cur_area = #pr_dest_no where taxi_id = NEW.gps_cab_bus;
update tbl_booking set booking_status = 9 where booking_pnr_no = #pr_pnr_no;
END;
ELSE IF #cab_meter_new = 0 and #cab_meter_old = 1 THEN
BEGIN
select `booking_pnr_no` into #pr_pnr_no from tbl_booking where booking_alloted_vehicle = (select taxi_id from tbl_taxi where taxi_imei = NEW.gps_imei ) AND booking_status = 9;
select trip_dst_area_id into #pr_dest_no from tbl_trip where pnr_id = #pr_pnr_no order by trip_id desc limit 0,1;
update tbl_taxi set taxi_cur_status = 10,taxi_cur_area = #pr_dest_no where taxi_id = NEW.gps_cab_bus;
update tbl_booking set booking_status = 8 where booking_pnr_no = #pr_pnr_no;
END;
END IF
END //
delimiter ;
try this code
delimiter //
CREATE TRIGGER `after_tblgps_insert` AFTER INSERT ON `tbl_gps`
FOR EACH ROW BEGIN
DECLARE cab_meter_new VARCHAR(10);
DECLARE cab_cost_new VARCHAR(10);
DECLARE cab_meter_old VARCHAR(10);
DECLARE cab_cost_old VARCHAR(10);
DECLARE pr_pnr_no VARCHAR(15);
DECLARE pr_dest_id VARCHAR(15);
select NEW.gps_rs232,NEW.gps_cab_bus,gps_rs232,gps_cab_bus into #cab_meter_new,#cab_cost_new,#cab_meter_old,#cab_cost_old from tbl_gps where gps_imei = NEW.gps_imei order by gps_id asc limit 0,1;
IF #cab_meter_new = 1 and #cab_meter_old = 0 THEN
BEGIN
select `booking_pnr_no` into #pr_pnr_no from tbl_booking where booking_alloted_vehicle = (select taxi_id from tbl_taxi where taxi_imei = NEW.gps_imei ) AND booking_status = 13;
select trip_dst_area_id into #pr_dest_no from tbl_trip where pnr_id = #pr_pnr_no order by trip_id desc limit 0,1;
update tbl_taxi set taxi_cur_status = 12,taxi_cur_area = #pr_dest_no where taxi_id = NEW.gps_cab_bus;
update tbl_booking set booking_status = 9 where booking_pnr_no = #pr_pnr_no;
END;
END IF;
IF #cab_meter_new = 0 and #cab_meter_old = 1 THEN//break IF into separate IF
BEGIN
select `booking_pnr_no` into #pr_pnr_no from tbl_booking where booking_alloted_vehicle = (select taxi_id from tbl_taxi where taxi_imei = NEW.gps_imei ) AND booking_status = 9;
select trip_dst_area_id into #pr_dest_no from tbl_trip where pnr_id = #pr_pnr_no order by trip_id desc limit 0,1;
update tbl_taxi set taxi_cur_status = 10,taxi_cur_area = #pr_dest_no where taxi_id = NEW.gps_cab_bus;
update tbl_booking set booking_status = 8 where booking_pnr_no = #pr_pnr_no;
END;
END IF;
END; //
delimiter ;
You have misstyped semicolon after END IF.