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.
Related
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 have been working on this for a while now and this seems to be too complex. What I want to do is to update a column (x) with the operation: (p * (100/c) ) / 100.
p corresponds to a value of a date x and c corresponds to a value of date x minus one day.
I tried to create a stored procedure with loop but select statement doesnt work for me in the loop statement.
Here is my procedure which update nothing :
BEGIN
DECLARE firstqDate,date2 date;
DECLARE p, c float;
DECLARE cpt, val int;
SET #val = 0;
SET #cpt = (select count(*)-1 from quotes);
WHILE (val < 3) DO
SET #firstqDate = (select qDate from quotes ORDER BY YEAR(qDate) ASC, MONTH(qDate) ASC, DAY(qDate) ASC limit 1,1);
SET date2 = (select qDate from quotes where qDate like DATE_ADD(#firstqDate, INTERVAL 1 DAY );
SET p = (select qOp from quotes where qDate like date2);
SET c = (select qCl from quotes where qDate like DATE_SUB(date2, INTERVAL val DAY));
update quotes
set qCh = (p * (100/c) ) / 100;
set val = val + 1;
end while;
END
EDIT : I did some updates to the stored procedure but still updating no lines!
BEGIN
DECLARE firstqDate,date2 date;
DECLARE p, c float;
DECLARE cpt, val int;
SET #val = 0;
SET #cpt = (select count(*)-1 from quotes);
SET firstqDate = (select qDate from quotes ORDER BY YEAR(qDate) ASC, MONTH(qDate) ASC, DAY(qDate) ASC limit 1,1);
WHILE (val < 3) DO
SET date2 = (select qDate from quotes where qDate like DATE_ADD(#firstqDate, INTERVAL val DAY ));
SET p = (select qOp from quotes where qDate like date2);
SET c = (select qCl from quotes where qDate like DATE_SUB(date2, INTERVAL val+1 DAY));
set val = val + 1;
update quotes
set qCh = (p * (100/c) ) / 100
where qOp = p AND qCl = c;
end while;
END
I did some updates again to the stored procedures but no changes. i used some functions.
BEGIN
DECLARE p, c float;
DECLARE cpt, val int;
SET #val = 0;
SET #cpt = (select count(*)-1 from quotes);
WHILE (#val < 3) DO
SET p = getp(#val, getd());
SET c = getc(#val+1, getd());
set #val = #val + 1;
update quotes
set qCh = (#p * (100/#c) ) / 100
where qOp = #p AND qCl = #c;
end while;
END
functions :
get p:
BEGIN
declare d date;
select qDate into d from quotes ORDER BY YEAR(qDate) ASC, MONTH(qDate) ASC, DAY(qDate) ASC limit 1,1;
return d;
END
get c:
BEGIN
DECLARE c float;
DECLARE qDa date;
select qDate into qDa from quotes where qDate like DATE_SUB(qD, INTERVAL v DAY );
SELECT qCl INTO c FROM quotes WHERE qDate = qDa;
RETURN c;
END
getd:
BEGIN
declare d date;
select qDate into d from quotes ORDER BY YEAR(qDate) ASC, MONTH(qDate) ASC, DAY(qDate) ASC limit 1,1;
return d;
END
this stored procedure must calculate all qCh from p of qDate and c of the qDate minus one day.
Thank you!
EDIT - Solved
Ouf! I finally managed to write this stored procedure :
BEGIN
DECLARE p, c float;
DECLARE cpt, val int;
SET #val = 0;
SET #cpt = (select count(*)-1 from quotes);
WHILE (#val <= 2) DO
SET p := getp(#val, getd());
SET c := getc(#val+1, getd());
set #val := #val + 1;
update quotes q
set q.qCh = (getp(#val, getd()) * (100/getc(#val-1, getd())) ) / 100
where q.qOp = getp(#val, getd());
end while;
END
new getC
BEGIN
DECLARE c float;
DECLARE qDa date;
select qDate into qDa from quotes where qDate like DATE_ADD(qD, INTERVAL v DAY );
SELECT qCl INTO c FROM quotes WHERE qDate = qDa;
RETURN c;
END
I changed the code of function getC to add (-1) in the first iteration. now it is working!
Thank you everyone for your help!
Your update cycle looks ok, even if i don't understand why you make 3 cycles. Shouldn't you use:
WHILE (#val < 3) DO
Instead of
WHILE (val < 3) DO
? Hope it helps
EDIT:
You need to debug your cycle to know where the problem is.
Try this:
BEGIN
DECLARE #p, #c float;
DECLARE #cpt, #val int;
SET #val = 0;
SET #cpt = (select count(*)-1 from quotes);
SELECT 'Enter Cycle';
WHILE (#val < 3) DO
SELECT 'In Cyle';
SET #p = getp(#val, getd());
SELECT #p;
SET c = getc(#val+1, getd());
set #val = #val + 1;
update quotes
set qCh = (#p * (100/#c) ) / 100
where qOp = #p AND qCl = #c;
end while;
END
Does you SP prints 'Enter Cycle' and 'In Cyle'? Does the value of p Variable prints? It is correct?
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;
I am using following stored procedure and calling a cursor inside it but it is returning me zero data fetched or processed what is wrong in the procedure ?
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `sp_ProcessData`(
DummyDate datetime
)
BEGIN
DECLARE MONTH BIGINT;
DECLARE YEAR BIGINT;
declare LBrCode VARCHAR(100);
declare EntryDate datetime;
declare VcrAcctId char(32);
declare DrCr VARCHAR(100);
declare FcyTrnAmt VARCHAR(100);
DECLARE CustName char(50);
DECLARE CreditAmount decimal(18,2);
DECLARE DebitAmount decimal(18,2);
DECLARE BatchCd char(15);
DECLARE SetNo BIGINT;
DECLARE ScrollNo BIGINT;
DECLARE BookType char(10);
DECLARE ActualAcctId varchar(50);
DECLARE ActualBrCode varchar(50);
DECLARE ActualBookType varchar(50);
declare curProcessAMLData cursor
for
select distinct * from DummyTable
WHERE EntryDate = DummyDate
AND (ActualBookType = 'XX');
open curProcessAMLData;
fetch next from curProcessAMLData into LBrCode,EntryDate,VcrAcctId,DrCr,FcyTrnAmt,BatchCd,SetNo,ScrollNo,BookType,ActualAcctId,ActualBrCode,ActualBookType;
while fetch_status = 0
DO
SET MONTH = MONTH(DummyDate);
SET YEAR = MONTH(DummyDate);
IF(DrCr = 'C')
THEN
SET CreditAmount = FcyTrnAmt;
IF NOT EXISTS (SELECT * FROM Master WHERE BranchCode = ActualBrCode AND AccNo = ActualAcctId AND TransMonth = MONTH and TransYear = YEAR)
THEN
INSERT INTO Master
(
TransDate,
BranchCode,
AccNo,
Credit,
TransMonth,
TransYear
)
SELECT
EntryDate,
ActualBrCode,
ActualAcctId,
CreditAmount,
MONTH,
YEAR
END;
ELSE
UPDATE Master
SET Credit = IFNULL(Credit,0) + CreditAmount
WHERE BranchCode = ActualBrCode AND AccNo = ActualAcctId
AND MONTH(TransDate) = MONTH
AND YEAR(TransDate) = YEAR;
END IF;
ELSE
SET DebitAmount = FcyTrnAmt;
IF NOT EXISTS (SELECT * FROM Master WHERE BranchCode = ActualBrCode AND AccNo = ActualAcctId AND TransMonth = MONTH and TransYear = YEAR)
THEN
INSERT INTO Master
(
TransDate,
BranchCode,
AccNo,
Debit,
TransMonth,
TransYear
)
SELECT
EntryDate,
ActualBrCode,
ActualAcctId,
DebitAmount,
MONTH,
YEAR
END;
ELSE
UPDATE Master
SET Debit = IFNULL(Debit,0) + DebitAmount
WHERE BranchCode = #ActualBrCode AND AccNo = ActualAcctId
AND MONTH(TransDate) = MONTH
AND YEAR(TransDate) = YEAR;
END IF;
END IF;
SET FcyTrnAmt = 0 ;
END WHILE;
fetch next from curProcessAMLData
into LBrCode,EntryDate,VcrAcctId,DrCr,FcyTrnAmt,BatchCd,SetNo,ScrollNo,BookType,ActualAcctId,ActualBrCode,ActualBookType;
CLOSE curProcessAMLData;
END
What changes will i have to make to make it working ?