What am I doing wrong?
CREATE DEFINER=`root`#`localhost` PROCEDURE `getAllPoints`()
BEGIN
declare done1, done2, done3, done4, pointAlive BOOLEAN DEFAULT FALSE;
*lots of declaring*
...
*lots of declaring*
declare cur1 cursor for select id, Owner_idOwner, longitude, lat,x,y from Point;
declare continue handler for not found set done1= TRUE;
DROP TEMPORARY TABLE IF EXISTS TEMP;
CREATE temporary TABLE IF NOT EXISTS TEMP (
*lots of stuff*
)ENGINE=MEMORY;
open cur1;
mainLoop: loop
fetch cur1 into pointid, ownerid, pointlong, pointlat, pointx,pointy;
if done1 then
set done1 = FALSE;
close cur1;
leave mainLoop;
end if;
insert into TEMP (тут работает);
BLOCK2:BEGIN
** Black is working right with cursor #1***
END BLOCK2 ;
BLOCK3:BEGIN
** Black is working right with cursor #2***
END BLOCK3;
That's what's not working:
pointid declared int with value "18045" fetched from cur1 cursor
call isAlive(pointid,#Alive,#timeTill);
update TEMP set Open = #Alive, TimeTill= #timeTill where Point_id = pointid;
end loop mainLoop;
select * from TEMP;
After calling isAlive loop seems to be interrupted and select query returns only 1 row with correct value in Open and TimeTill column instead of ~1200rows with correct values when isAlive is not called
END
This is the isAlive stored procedure:
CREATE DEFINER=`root`#`localhost` PROCEDURE `isAlive`(IN iPoint_id int, INOUT Alive int, INOUT timeTill time)
BEGIN
declare pointAlive int;
select count(id) into pointAlive from Schedule where
day = dayofweek(curdate())-1
and Point_id = iPoint_id
and Start < curtime()
and Stop > curtime();
if poinTalive then
select pointAlive,timediff(Stop,curtime()) INTO Alive, timeTill
from Points.Schedule
where day = dayofweek(curdate()) -1
and Point_id = iPoint_id;
else
select pointAlive,timediff(str_to_date(concat(curdate(),' ',#starttime), "%Y-%m-%d %T"),now()) INTO Alive, timeTill
from Points.Schedule
where day = dayofweek(curdate()) -1
and Point_id = iPoint_id;
end if;
END
Can you tell me what am I doing wrong? How can I call isAlive for every pointid ? Thank you
Related
I am very used to writing in TSQL and MySQL is taking some adjusting but I think I am getting most of it. I have one stored procedure however that will not budge, I have read and re-read it 100 times, read up all I can find on the error and still no luck.
On Line 11 and the end of this statement:
CREATE TEMPORARY TABLE ReturnAvalRooms (roomID int);
I get the error "Missing End" even though this is at the beginning of my query.
I have checked and double checked that everything is Declared and Set with appropriate ; but just can't figure out why I am getting this error.
Below is a full copy of the Stored Procedure.
CREATE PROCEDURE `Get_AvailRooms` (IN StartDate datetime, IN EndDate datetime, IN RoomType int)
BEGIN
DECLARE PStartDate datetime;
DECLARE PEndDate datetime;
DECLARE PRoomType int;
SET PStartDate = StartDate;
SET PEndDate = EndDate;
SET PRoomType = RoomType;
CREATE TEMPORARY TABLE AvaliableNowRooms AS (SELECT idRooms FROM rooms WHERE RoomNextAvail < PStartDate AND RoomTypeID = PRoomType);
CREATE TEMPORARY TABLE ReturnAvalRooms (roomID int);
DECLARE AvailRooms CURSOR FOR SELECT * FROM AvaliableNowRooms;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
DECLARE RoomID INT;
OPEN AvailRooms;
read_loop: LOOP
FETCH AvailRooms INTO RoomID;
IF done THEN
LEAVE read_loop;
END IF;
DECLARE Bookings CURSOR FOR SELECT BookingStartDate, BookingEndDate, BookingRoomID FROM Booking WHERE BookingRoomID = RoomID AND BookingEndDate < PStartDate;
DECLARE PBookingStartDate datetime;
DECLARE PBookingEndDate datetime;
DECLARE PBookingRoomID,
OPEN Bookings
Booking_Read: LOOP
FETCH Bookings INTO PBookingStartDate, PBookingEndDate, PBookingRoomID;
IF done THEN
LEAVE Booking_Read;
END IF;
IF PBookingStartDate BETWEEN PStartDate AND PEndDate
LEAVE Booking_Read;
ELSE IF PBookingEndDate BETWEEN PStartDate AND PEndDate
LEAVE Booking_Read;
ELSE
INSERT INTO ReturnAvalRooms (id) values (PBookingRoomid);
END LOOP;
CLOSE Bookings;
END
END LOOP;
CLOSE AvailRooms;
END
SELECT * FROM ReturnAvalRooms
END
MySQL is particular about the order of the DECLARE statements. Within a block, those have to appear first. And (I believe) the HANDLERs have to be the last declarations.
I'm confused by what your procedure is intending to return. It seems like you'd want to return all rooms of the specified type for which there isn't any booking that overlaps the specified period.
But looking at the logic in the procedure, it seems like if there are no rows in booking for a given room, that room won't be returned. And that seems odd. And that has me puzzled about what this is really supposed to return. The specification is obfuscated by a lot of unnecessary clutter.
The whole rigmarole could be accomplished by a single SQL statement which is much simpler, much clearer, and more efficient to boot.
DELIMITER $$
CREATE PROCEDURE `Get_AvailRooms` (IN PStartDate datetime, IN PEndDate datetime, IN PRoomType int)
BEGIN
-- rooms of the specified type which are "available" for the
-- specified period. A room is considered not available if
-- the RoomNextAvail col has a date value later than the beginning
-- of the specified period, or there are one or more bookings that
-- overlap that period. If the specified StartDate is later than
-- the specified EndDate, then no rooms are available.
--
-- This query uses an "anti-join" pattern to return only rows
-- in rooms which don't have a overlapping booking
SELECT r.idRooms
FROM rooms r
LEFT
JOIN Booking b
ON b.BookingRoomID = r.idRooms
AND b.BookingEndDate >= PStartDate
AND b.BookingStartDate <= PEndDate
WHERE b.idRooms IS NULL
AND r.RoomTypeID = PRoomType
AND r.RoomNextAvail < PStartDate
AND PStartDate < PEndDate
ORDER BY r.idRooms;
END$$
DELIMITER ;
That's just a first cut; it's not tested. We might want to change the <= and >= to be just < and >, depending on whether or not we consider a booking that ends at exactly 10AM overlaps with a period starting right at 10AM.
Declare variables at the top of the procedure. Also you are not using THEN after IF statement. Correct the code. Also, you are closing the loop before closing the IF block.
CREATE PROCEDURE `Get_AvailRooms` (IN StartDate datetime, IN EndDate datetime, IN RoomType int)
BEGIN
DECLARE PStartDate datetime;
DECLARE PEndDate datetime;
DECLARE PRoomType int;
DECLARE AvailRooms CURSOR FOR SELECT * FROM AvaliableNowRooms;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
DECLARE RoomID INT;
DECLARE Bookings CURSOR FOR SELECT BookingStartDate, BookingEndDate, BookingRoomID FROM Booking WHERE BookingRoomID = RoomID AND BookingEndDate < PStartDate;
DECLARE PBookingStartDate datetime;
DECLARE PBookingEndDate datetime;
DECLARE PBookingRoomID,
SET PStartDate = StartDate;
SET PEndDate = EndDate;
SET PRoomType = RoomType;
CREATE TEMPORARY TABLE AvaliableNowRooms AS (SELECT idRooms FROM rooms WHERE RoomNextAvail < PStartDate AND RoomTypeID = PRoomType);
CREATE TEMPORARY TABLE ReturnAvalRooms (roomID int);
OPEN AvailRooms;
read_loop: LOOP
FETCH AvailRooms INTO RoomID;
IF done THEN
LEAVE read_loop;
END IF;
OPEN Bookings
Booking_Read: LOOP
FETCH Bookings INTO PBookingStartDate, PBookingEndDate, PBookingRoomID;
IF done THEN
LEAVE Booking_Read;
END IF;
IF PBookingStartDate BETWEEN PStartDate AND PEndDate THEN
LEAVE Booking_Read;
ELSE IF PBookingEndDate BETWEEN PStartDate AND PEndDate THEN
LEAVE Booking_Read;
ELSE
INSERT INTO ReturnAvalRooms (id) values (PBookingRoomid);
END;
END LOOP;
CLOSE Bookings;
END
END LOOP;
CLOSE AvailRooms;
END
SELECT * FROM ReturnAvalRooms
END
So I'm trying to create a stored procedure in MySQL version 5.5.
I'm not sure what is wrong but what I want to accomplish is.
Get record From Table-A that over 7 days old. And then insert into Table-B, but I need to check if it is exist in Table B. If it is exists then skip, else Insert it.
So here is my code:
DROP PROCEDURE IF EXISTS `move_record`;
DELIMITER //
CREATE PROCEDURE `move_record`()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE dt DATETIME;
DECLARE uid,value BIGINT(20);
DECLARE category VARCHAR(30);
DECLARE data,comments VARCHAR(255);
DECLARE cancel TINYINT(1) DEFAULT NULL;
DECLARE curs CURSOR FOR SELECT `datetime`,user_id,category,data,comments,cancel FROM `record` WHERE `datetime` < DATE_SUB(CURDATE(), INTERVAL 7 DAY);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN curs;
myloop: LOOP
FETCH NEXT FROM curs INTO dt,uid,category,data,comments,cancel;
IF done THEN
LEAVE myloop;
END IF;
IF NOT EXISTS (SELECT * FROM `record_arc`
WHERE record_arc.`datetime` = dt
AND record.user_id = uid )
INSERT INTO `record_arc` (`datetime`,user_id,category,data,comments,cancel) VALUES (dt,uid,category,data,comments,cancel);
END IF;
END LOOP myloop;
CLOSE curs;
DEALLOCATE curs;
END//
DELIMITER ;
Maybe you can do this without a loop.
INSERT INTO `record_arc`(
`datetime`,
user_id,
category,
data,
comments,
cancel
)
SELECT
`datetime`,
user_id,
category,
data,
comments,
cancel
FROM `record` r
WHERE
`datetime` < DATE_SUB(CURDATE(), INTERVAL 7 DAY)
AND NOT EXISTS(
SELECT 1
FROM `record_arc` r2
WHERE
r2.`datetime` = r.`datetime`
AND r2.user_id = r.user_id
)
Alias name you used it wrongly. Check the below code
IF NOT EXISTS (SELECT 1 FROM `record_arc`
WHERE record_arc.`datetime` = dt
AND record_arc.user_id = uid )
I have been debugging a SQL stored procedure which has to take values (in my code ID and Numb) form table A based on the values (ID) present in the Table C, then square the Numb and store it in Table B i.e. all the things ID, Numb and Square.
I am not able to figure out the problem in the below code
DELIMITER $$
CREATE PROCEDURE matlab.squaring
BEGIN
DECLARE finish BOOLEAN DEFAULT 0; # <- set up initial conditions
DECLARE square BIGINT(10);
DECLARE ID INT(10);
DECLARE Numb INT (10);
DECLARE id_cur CURSOR FOR
SELECT ID, Numb FROM A WHERE EXISTS ( SELECT ID FROM c);
SET #square= #Numb * #Numb
INSERT INTO B
(
ID ,
Numb ,
square
) values ( ID , Numb, square);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finish = TRUE;
OPEN id_cur;
the_loop : LOOP
FETCH id_cur INTO ID;
IF finish THEN
CLOSE id_cur;
LEAVE the_loop;
END IF
END LOOP the_loop;
END$$
When I run the stored procedure the error that pops up is "there seems to be some syntax error in your code, please refer to MYSql guide. "
edit:
one more help please how to execute this stored procedure.
There are various minor errors;
You need a parameter list, even if empty for the procedure;
CREATE PROCEDURE matlab.squaring()
The continue handler needs to be right below the other declarations;
DECLARE id_cur CURSOR FOR
SELECT ID, Numb FROM A WHERE EXISTS ( SELECT ID FROM c);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET #finish = TRUE;
You forgot a semicolon;
SET #square= #Numb * #Numb;
You forgot # on the variable usages;
) values ( #ID , #Numb, #square);
You forgot a semicolon on END IF
END IF;
Just as an overview, here's the complete thing updated;
CREATE PROCEDURE matlab.squaring()
BEGIN
DECLARE finish BOOLEAN DEFAULT 0; # <- set up initial conditions
DECLARE square BIGINT(10);
DECLARE ID INT(10);
DECLARE Numb INT (10);
DECLARE id_cur CURSOR FOR
SELECT ID, Numb FROM A WHERE EXISTS ( SELECT ID FROM c);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET #finish = TRUE;
SET #square= #Numb * #Numb;
INSERT INTO B
(
ID ,
Numb ,
square
) values ( #ID , #Numb, #square);
OPEN id_cur;
the_loop : LOOP
FETCH id_cur INTO ID;
IF finish THEN
CLOSE id_cur;
LEAVE the_loop;
END IF;
END LOOP the_loop;
END//
You have missed () after PROCEDURE matlab...
And ; after END IF
Also, HANDLER declaration should be before any executable code and
after CURSOR declaration
Semicolon after SET #square= #Numb * #Numb is needed
So, query should be like this:
DELIMITER $$
CREATE PROCEDURE matlab.squaring ()
BEGIN
DECLARE finish BOOLEAN DEFAULT 0; # <- set up initial conditions
DECLARE square BIGINT(10);
DECLARE ID INT(10);
DECLARE Numb INT (10);
DECLARE id_cur CURSOR FOR
SELECT ID, Numb FROM A WHERE EXISTS ( SELECT ID FROM c);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finish = TRUE;
SET #square= #Numb * #Numb;
INSERT INTO B
(
ID ,
Numb ,
square
) values ( ID , Numb, square);
OPEN id_cur;
the_loop : LOOP
FETCH id_cur INTO ID;
IF finish THEN
CLOSE id_cur;
LEAVE the_loop;
END IF;
END LOOP the_loop;
END$$
Missed parameter brackets and semicolon at endif.
DELIMITER $$
CREATE PROCEDURE squaring()
BEGIN
DECLARE finish BOOLEAN DEFAULT 0; # <- set up initial conditions
DECLARE square BIGINT(10);
DECLARE ID INT(10);
DECLARE Numb INT (10);
DECLARE id_cur CURSOR FOR
SELECT ID, Numb FROM A WHERE EXISTS ( SELECT ID FROM c);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finish = TRUE;
SET #square= #Numb * #Numb;
INSERT INTO B
(
ID ,
Numb ,
square
) values ( ID , Numb, square);
OPEN id_cur;
the_loop : LOOP
FETCH id_cur INTO ID;
IF finish THEN
CLOSE id_cur;
LEAVE the_loop;
END IF;
END LOOP the_loop;
END$$
Iam Using following code to run this event using
IF CURRENT_TIME() = '23:50:00'
But doing so, every second it has to compare the time with server time. can we implement this with out if condistion
drop event OEAuditEvent;
DELIMITER $$
CREATE EVENT OEAuditEvent
ON SCHEDULE EVERY 1 SECOND
STARTS '2012-08-30 09:00:10'
DO
BEGIN
DECLARE a CHAR(20);
DECLARE b,c,d INT;
DECLARE done INT DEFAULT FALSE;
IF CURRENT_TIME() = '23:50:00' THEN
begin
DECLARE cur CURSOR FOR select OE_User,count(OE_User) from RNCM_Status where date(OE_Date)=CURDATE() group by OE_User;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
read_loop: LOOP
FETCH cur INTO a, b;
SET c=ceil((b*5)/100);
insert into test values(a,b);
IF done THEN
LEAVE read_loop;
ELSE
insert into OE_Audit(MDN,CAF,UploadedDate,OEUser,OEDate,UserCount,QCCount,intime) select MDN,CAF,UploadedDate,OE_User,OE_Date,b,c,now() from RNCM_Status where OE_User=a and date(OE_Date)=CURDATE() order by rand() limit c;
END IF;
END LOOP;
CLOSE cur;
end ;
END IF;
END $$
DELIMITER ;
Why exactly not create the event itself to run daily at 23:00 and remove IF completely?
CREATE EVENT OEAuditEvent
ON SCHEDULE EVERY 1 DAY
STARTS '2012-08-30 23:00:00'
DELIMITER //
DROP PROCEDURE IF EXISTS sp_vk_suspend//
CREATE PROCEDURE sp_vk_suspend2()
declare today = DATETIME DEFAULT NULL;
declare accid = INT DEFAULT 11;
set today = now();
BEGIN
SELECT * FROM members where expirydate < today;
END //
This is my stored procedure. I have to add one or more query depend with id from members table. How can I retrieve id from members table.
ex: I have to add this query in my sp
UPDATE members
SET suspend = 1
WHERE id = 'some id from mem: tables'
i see IMHO cursors it's what you need
DECLARE done INT DEFAULT FALSE;
DECLARE cur1 CURSOR FOR SELECT id FROM members where expirydate < today;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
DECLARE a INT;
OPEN cur1;
my_loop: LOOP
FETCH cur1 INTO a
IF done THEN
LEAVE my_loop;
END IF;
UPDATE members SET suspend=1 WHERE id = a;
INSERT INTO another(member_id) values(a);
END LOOP;
CLOSE cur1;