I am writing my first mysql procedure. I have a date variable and 2 integer variables. I am multiplying the two integer variables and adding the resultant integer to the date variable.
For example:
pint=6;
noftimes=3
sdate=2020-05-05
totDays=pint*noftimes
edate=2020-05-05+totDays
I am able to multiple pint*noftimes but not add sdate+totDays. Whereas If I add sdate+10 then I am getting a correct incremental date value. The following is my procedure:
DELIMITER $$
CREATE DEFINER=`pattu`#`localhost` PROCEDURE `getFieldWorkDates`(IN `p_id` INT, OUT `totDays` INT, OUT `edate` DATE)
BEGIN
DECLARE noftimes int;
DECLARE pint int;
DECLARE sdate DATE;
DECLARE tdays int;
SELECT startDate into sdate from projects where idprojects=p_id;
SELECT projectDuration into noftimes from projects where idprojects=p_id;
SELECT recFreq into pint from projects where idprojects=p_id;
SET totDays=pint*noftimes;
SET edate = sdate+(pint*noftimes);
END$$
DELIMITER ;
When I execute this, I am getting the message, your query has been executed successfully. 0 rows affected
SET edate = sdate+(pint*noftimes);
You cannot add integer to date. Use DATE_ADD() function.
CREATE
DEFINER=pattu#localhost
PROCEDURE getFieldWorkDates ( IN p_id INT,
OUT totDays INT,
OUT edate DATE)
SELECT recFreq * projectDuration,
startDate + INTERVAL recFreq * projectDuration DAY
INTO totDays,
edate
FROM projects
WHERE idprojects=p_id;
Hi i am trying to run a a loop in stored procedure where in i get the sum of the values from the select query based upon the date(curr_date).
I need to loop the date column in my select query(curr_date).
The problem that i am facing is that when i run the while loop it only gets me the 1st entry for the date from the database.
My Mysql stored procedure is as follows:
CREATE PROCEDURE `paf_calculation`(out avg_sum1 float(15,3))
BEGIN
DECLARE day_count int;
DECLARE extra_days int;
DECLARE lookup_weeks int;
DECLARE week_days INT;
DECLARE count decimal;
DECLARE datapoints int;
DECLARE start_date date;
DECLARE previous_days date;
DECLARE sum_extra_days float(15,3);
DECLARE roid int;
DECLARE historylookup_weeks int;
DECLARE avg_sum float;
DECLARE i int;
DECLARE curr_date date;
set i = 0;
SET WEEK_DAYS = 7;
Set count = 0.5;
select datediff('2016-10-10','2016-10-7') into historylookup_weeks;
/** counts the number of days from the current week start and end days */
select datediff('2016-10-10','2016-10-7') into day_count;
/**No of extra days if the current week is more or less than 7 */
SET extra_days = day_count - WEEK_DAYS;
/**calculates the date 56 days from the current start week date */
select date_sub('2016-10-17',interval 56 day) into previous_days ;
/**calculates the datapoints*/
set avg_sum1=0;
Set datapoints = day_count * lookup_weeks;
set i = 0;
set curr_date = '2016-10-08';
while curr_date < '2016-10-17' do
SELECT SUM(actual_volume) into avg_sum FROM volume_trackers WHERE (date_inserted between '2016-08-23' AND '2016-10-17') AND (day_of_week in (dayofweek(curr_date))) and (ro_id=13);
set curr_date=date_add(curr_date,interval 1 day);
set avg_sum1=avg_sum+avg_sum1;
END WHILE;
END
Your query does seem to loop as coded but I would amend the sum_avg1 calculation to allow for nulls returned to avg sum
set avg_sum1=coalesce(avg_sum,0) + avg_sum1;
I have a medium sized stored procedure going on here below. My problem is that it doesn't do anything and I have no idea why.
1.) First of all, the code:
DROP PROCEDURE IF EXISTS deleteabundant_fixshared_shiftResources;
DELIMITER //
CREATE PROCEDURE deleteabundant_fixshared_shiftResources ()
BEGIN
DECLARE finish_flag BOOLEAN DEFAULT FALSE;
DECLARE id INT(11);
DECLARE startTime DATETIME;
DECLARE endTime DATETIME;
DECLARE shid INT(11);
DECLARE resid INT(11);
DECLARE id_inner INT(11);
DECLARE startTime_inner DATETIME;
DECLARE endTime_inner DATETIME;
DECLARE shid_inner INT(11);
DECLARE resid_inner INT(11);
DECLARE cr130 CURSOR FOR SELECT shift_resource_id, start_date, end_date, shift_id, resource_id FROM temp_shift_resource;
DECLARE cr131 CURSOR FOR SELECT shift_resource_id, start_date, end_date, shift_id, resource_id FROM temp_shift_resource;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finish_flag = TRUE;
START TRANSACTION;
OPEN cr130;
OPEN cr131;
OUTERLOOP: LOOP
FETCH cr130 into id, startTime, endTime, shid, resid;
IF finish_flag THEN LEAVE OUTERLOOP; END IF;
INNERLOOP: LOOP
FETCH cr131 INTO id_inner, startTime_inner, endTime_inner, shid_inner, resid_inner;
IF finish_flag THEN LEAVE INNERLOOP; END IF;
IF (id!=id_inner) THEN
IF (resid=resid_inner AND shid_inner!=9) THEN
-- logic to determine if the dates are wrong:
IF (startTime<=startTime_inner AND endTime>=endTime_inner) THEN
INSERT INTO repairchange ( shift_resource_id, changetype, shift_id, resource_id, start_date, end_date )
VALUES ( id_inner, "FD", shid_inner, resid_inner, startTime_inner, endTime_inner );
DELETE FROM temp_shift_resource WHERE shift_resource_id = id_inner;
ELSEIF (endTime>=endTime_inner AND startTime<=endTime_inner) THEN
INSERT INTO repairchange ( shift_resource_id, changetype, shift_id, resource_id, start_date, end_date )
VALUES ( id_inner, "FU", shid_inner, resid_inner, startTime_inner, endTime_inner );
UPDATE temp_shift_resource set endTime_inner=(startTime - INTERVAL 1 DAY) where shift_resource_id = id_inner;
ELSEIF (startTime<=startTime_inner AND endTime>=startTime_inner) THEN
INSERT INTO repairchange ( shift_resource_id, changetype, shift_id, resource_id, start_date, end_date )
VALUES ( id_inner, "FU", shid_inner, resid_inner, startTime_inner, endTime_inner );
UPDATE temp_shift_resource set startTime_inner=(endTime + INTERVAL 1 DAY) where shift_resource_id = id_inner;
END IF;
END IF;
END IF;
END LOOP INNERLOOP;
SET finish_flag = FALSE;
END LOOP OUTERLOOP;
CLOSE cr130;
CLOSE cr131;
COMMIT;
END //
DELIMITER ;
call deleteabundant_fixshared_shiftResources();
2.) Description of what I want to do:
Basically, I have a table full of workshifts. Due to code bugs, some of these shifts have a wrong date assigned to them, and I have to fix the database.
I have to run through the whole table, and compare the rows that are assigned to the same resource_id, which represents a person. So if a person has two shifts that look like (2016-05-10 to 2016-05-20) and (2016-05-15 to 2016-05-23) for example, I have to fix it so that one of them will be trimmed to (2016-05-10 to 2016-05-14) and (2016-05-15 to 2016-05-23).
A shift that is a nightshift, marked as shift_id=9, must not be modified at all.
I insert rows into the repairchange table if a change or a deletion has been made
3.) The procedure runs, but does nothing. I have examples in the database for wrong rows, one example is the one I wrote above. I suspect it is the nested loop, because I want to loop and fetch through the same table, but I haven't found anything on that.
I got the message
0 row(s) affected, 1 warning(s): 1329 No data - zero rows fetched, selected, or processed
but I have seen this before and my stored procedures have worked even though they output this warning.
Any ideas or tips are welcome. Thank you for your time!
I figured it out, after quite some debugging:
I opened the cursors before both of the loops. This meant that after the first walk-through of the inner loop, the cursor was standing at +1 of the LAST row of the table, and when the new outer loop iteration started the second inner loop iteration, the cursor was still at the end position.
Thus it did not run. I replaced the inner-cursor opening and closing into the outer loop, and now it works properly.
I am very new to mysql scripts , I want to execute this query by incrementing 00:00:00 time to 30 minutes .
something like this
Select count(*)
FROM ctrdb.CTR_LINE_ITEM
where LOAD_DATE BETWEEN '2016-05-18 00:00:00' AND '2016-05-18 00:30:00'
order by load_date;
Select count(*)
FROM ctrdb.CTR_LINE_ITEM
where LOAD_DATE BETWEEN '2016-05-18 00:30:00' AND '2016-05-18 00:60:00'
order by load_date;
Can you guys please help me ?
if you wan to achieve this in mysql and want to get separate resultset for each query
then you need to run your query in loop by using stored procedure.
read loop in mysql http://www.mysqltutorial.org/stored-procedures-loop.aspx
or there are not multiple queries then you can use union as well
DELIMITER $$
CREATE PROCEDURE getdata()
BEGIN
DECLARE x INT;
DECLARE maximum INT; # you can use date as well
DECLARE startdate DATE;
DECLARE enddate DATE;
SET x =0;
SET maximum = 10;
SET startdate = '2016-05-18 00:00:00';
loop_label: LOOP
IF x > 10 THEN
LEAVE loop_label;
END IF;
SET x = x + 1;
SET enddate = startdate + INTERVAL 30 MINUTE;
Select count(*) ,startdate
FROM ctrdb.CTR_LINE_ITEM
where LOAD_DATE BETWEEN '2016-05-18 00:00:00' AND '2016-05-18 00:30:00'
order by load_date;
SET startdate = startdate + INTERVAL 30 MINUTE;
END LOOP;
END $$
DELIMITER ;
My table has a column date_period which stores Date in the format YYYYMM. I want to write a query which inserts date in date_period column in the format YYYYMM if there is no entry till current month.
For Example: the date period has entry till October 2015 so it will contain the value 201510. Now I want to check and insert data till current month if it is not present. So the entries will be now 201511, 201512, 201601
How can I achieve this?
try this way, may it will help you
DECLARE #v DATE= getdate()
declare #Currentdate varchar(10)
set #Currentdate=CONVERT(VARCHAR(10), #v, 112)
/*#Currentdate string is in format '20160129'*/
IF NOT EXISTS(select * from Table1 where date_period=LEFT(#Currentdate,6))
begin
insert into Table1(date_period)values(LEFT(#Currentdate,6))
end
Try this
i think this will be the complete solution for your problem
declare #monthcount int
DECLARE #v DATE= getdate()
declare #lastsavedMonth varchar(20)= (select MAX(date_period) from Table1)
declare #Lastsaveddate varchar(20)= #lastsavedMonth+''+RIGHT(CONVERT(VARCHAR, 100 + DATEPART(d,#v)), 2)
set #monthcount=datediff(month,#Lastsaveddate,CONVERT(VARCHAR(10), #v, 112))
while #monthcount>=0
begin
declare #dateofsavingmonth varchar(20)
if(#monthcount=0)
begin
set #dateofsavingmonth=CONVERT(VARCHAR(10),#v,112)
end
else
begin
set #dateofsavingmonth=CONVERT(VARCHAR(10), dateadd(month,-#monthcount,#v),112)
end
IF NOT EXISTS(select * from Table1 where date_period=LEFT(#dateofsavingmonth,6))
begin
insert into Table1(date_period)values(LEFT(#dateofsavingmonth,6))
end
set #monthcount =#monthcount-1
end