MySQL Stored Procedure Delimiter - mysql

I try to create following procedure:
DROP PROCEDURE IF EXISTS my_super_procedure;
DELIMITER $$
CREATE PROCEDURE my_super_procedure(IN in_id VARCHAR(255), IN in_from_date timestamp, IN in_to_date timestamp)
BEGIN
DROP TEMPORARY TABLE IF EXISTS tblResults;
CREATE TEMPORARY TABLE IF NOT EXISTS tblResults(
selected_date timestamp,
total_value int
); -- !!!!! Syntax error: missing END
DECLARE tmp_date timestamp;
SET tmp_date = in_from_date;
WHILE tmp_date < in_to_date DO
SELECT count(*) as total_events FROM event e where e.scheduled between tmp_date and tmp_date + interval 1 day;
set tmp_date = DATE_ADD(tmp_date, INTERVAL 1 DAY);
END WHILE;
END$$
DELIMITER ;
call my_super_procedure("3dccd75a-4c8e-11e7-bf68-5ce0c56861d1", "2017-06-24 04:16:43", "2017-06-28 04:16:43");
I'm new in stored procedures.
How can I fix this error?

Declare all variables at the beginning, not in the middle of your code.
DROP PROCEDURE IF EXISTS my_super_procedure;
DELIMITER $$
CREATE PROCEDURE my_super_procedure(IN in_id VARCHAR(255), IN in_from_date timestamp, IN in_to_date timestamp)
BEGIN
DECLARE tmp_date timestamp;
DROP TEMPORARY TABLE IF EXISTS tblResults;
CREATE TEMPORARY TABLE IF NOT EXISTS tblResults(
selected_date timestamp,
total_value int
);
SET tmp_date = in_from_date;
WHILE tmp_date < in_to_date DO
SELECT count(*) as total_events FROM event e where e.scheduled between tmp_date and tmp_date + interval 1 day;
set tmp_date = DATE_ADD(tmp_date, INTERVAL 1 DAY);
END WHILE;
END$$
DELIMITER ;

Related

sql scheduler to update balance leaves

This is my credit leave table.
creditleaveid empid creditleavemonth creditedleaves
1 21 March 1
I am using scheduler to insert next month to credit/balance leave in this table is
DELIMITER $$
CREATE EVENT creditleavemonth
ON SCHEDULE EVERY '1' MONTH
STARTS '2017-02-01 00:00:00'
DO
BEGIN
CALL spAddCreditLeaves;
END$$
DELIMITER ;
stored procedure spAddCreditLeaves is
DELIMITER $$
USE `attendance`$$
DROP PROCEDURE IF EXISTS `spAddCreditLeaves`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `spAddCreditLeaves`()
BEGIN
INSERT INTO creditleaves(empid,creditleavemonth)SELECT empid,MONTHNAME(CURDATE())FROM empdetails;
END$$
DELIMITER ;
Now I want to update a record of inserted month by checking previous month's record for every employee. How to do that?
I have got my answer. It is working awesomely. stored procedure-
USE `attendance`$$
DROP PROCEDURE IF EXISTS `spAddCreditLeaves`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `spAddCreditLeaves`()
BEGIN
IF MONTHNAME(CURDATE())='January'
THEN
INSERT INTO creditleaves(empid,creditleavemonth,creditmonthyear,creditedleaves) SELECT empid,MONTHNAME(CURDATE()),YEAR(CURDATE()),creditedleaves+1 FROM creditleaves WHERE empid IN (SELECT empid FROM empdetails GROUP BY empid) AND creditedleaves IN(SELECT creditedleaves FROM creditleaves WHERE creditleavemonth=MONTHNAME((CURDATE()-INTERVAL 1 MONTH)) AND creditmonthyear=YEAR((CURDATE()-INTERVAL 1 YEAR)));
ELSE
INSERT INTO creditleaves(empid,creditleavemonth,creditmonthyear,creditedleaves) SELECT empid,MONTHNAME(CURDATE()),YEAR(CURDATE()),creditedleaves+1 FROM creditleaves WHERE empid IN (SELECT empid FROM empdetails GROUP BY empid) AND creditedleaves IN(SELECT creditedleaves FROM creditleaves WHERE creditleavemonth=MONTHNAME((CURDATE()-INTERVAL 1 MONTH)) AND creditmonthyear=YEAR(CURDATE()));
END IF;
END$$
DELIMITER ;

mysql syntax error while set stored procedure parameter

I written the code for mysql stored procedure it shows syntax error i didn't know what mistake i done. anyone help me please.
DELIMITER $$
DROP PROCEDURE IF EXISTS `aad_adr`.` PROCEDURE MonthlySalesReport(fromdate DATE,todate DATE)` $$
CREATE PROCEDURE `aad_adr`.` PROCEDURE MonthlySalesReport(fromdate DATE,todate DATE)` ()
BEGIN
Declare fd DATE;
Declare ed DATE;
SET fd=fromdate;
SET ed=todate;
WHILE DATE(fd)<=DATE(ed)DO
select bill_master.bill_no,DATE_FORMAT(bill_master.bill_date, '%y/%m/%d') AS 'formatted_date',transaction.product_id,transaction.tax_amount,transaction.amount,transaction.amount-transaction.tax_amount as 'without_tax ',product_master.Product_name,product_master.vat from bill_master inner join transaction on bill_master.bill_no=transaction.bill_no inner join product_master on transaction.product_id=product_master.product_id where vat='14.50' and DATE_FORMAT(bill_master.bill_date, '%y/%m/%d') fd;
SET fd=DATE_ADD(fd,INTERVAL 1 DAY);
END WHILE;
END $$
DELIMITER;
error:
Script line: 4 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 'fd;
SET fd=DATE_ADD(fd,INTERVAL 1 DAY);
END WHILE;
END' at line 8
Error in your select query you are calling wrong where condition. So Just remove DATE_FORMAT in where clause And need to one more modification just replace DELIMITER; to DELIMITER$$ in last.
Update 1: Modified Valid name of SP.
So Updated Code is:
DELIMITER $$
DROP PROCEDURE IF EXISTS `aad_adr`.`MonthlySalesReport` $$
CREATE PROCEDURE `aad_adr`.`MonthlySalesReport`(IN fromdate DATE,IN todate DATE)
BEGIN
DECLARE fd DATE;
DECLARE ed DATE;
SET fd=fromdate;
SET ed=todate;
WHILE DATE(fd)<=DATE(ed)DO
SELECT bill_master.bill_no,DATE_FORMAT(bill_master.bill_date, '%y/%m/%d') AS 'formatted_date',transaction.product_id,transaction.tax_amount,transaction.amount,transaction.amount-transaction.tax_amount AS 'without_tax ',product_master.Product_name,product_master.vat FROM bill_master INNER JOIN TRANSACTION ON bill_master.bill_no=transaction.bill_no INNER JOIN product_master ON transaction.product_id=product_master.product_id WHERE vat='14.50' AND bill_master.bill_date=fd;
SET fd=DATE_ADD(fd,INTERVAL 1 DAY);
END WHILE;
END $$
DELIMITER$$
First you have two erreur:
the first one is the name of PROCEDURE
CREATE PROCEDURE `aad_adr`.` PROCEDURE MonthlySalesReport(fromdate DATE,todate DATE)` ()
--- >
CREATE PROCEDURE `aad_adr`.`MonthlySalesReport`(fromdate DATE,todate DATE)
so you procedure should be like this :
DELIMITER $$
DROP PROCEDURE IF EXISTS `aad_adr`.`MonthlySalesReport`$$
CREATE PROCEDURE `aad_adr`.`MonthlySalesReport`(fromdate DATE,todate DATE)
BEGIN
Declare fd DATE;
Declare ed DATE;
SET fd=fromdate;
SET ed=todate;
WHILE DATE(fd)<=DATE(ed)DO
select
bill_master.bill_no,
DATE_FORMAT(bill_master.bill_date,'%y/%m/%d') AS 'formatted_date',
transaction.product_id,
transaction.tax_amount,
transaction.amount,
transaction.amount-transaction.tax_amount as 'without_tax ',
product_master.Product_name,
product_master.vat
from
bill_master inner join transaction on bill_master.bill_no=transaction.bill_no
inner join product_master on transaction.product_id=product_master.product_id
where
vat='14.50' and vat='14.50' and DATE_FORMAT(bill_master.bill_date, '%y/%m/%d');
SET fd=DATE_ADD(fd,INTERVAL 1 DAY);
END WHILE;
END $$
DELIMITER ;

error shown call mysql stored procedure in query browser

I have created a stored procedure in mysql. When I call the procedure in mysql query browser it shows error "procedure dose not exist" but I am sure that the stored procedure exists in the database.I don't know where I am going wrong. Please help anyone
This is my Stored Procedure Code
DELIMITER $$
DROP PROCEDURE IF EXISTS `aad_adr`.` MonthlySalesReport(IN fromdate DATE,IN todate DATE)` $$
CREATE PROCEDURE `aad_adr`.` MonthlySalesReport(IN fromdate DATE,IN todate DATE)` ()
BEGIN
Declare fd DATE;
Declare ed DATE;
SET fd=fromdate;
SET ed=todate;
WHILE DATE(fd)<=DATE(ed)DO
select bill_master.bill_no,DATE_FORMAT(bill_master.bill_date, '%y/%m/%d') AS 'formatted_date',transaction.product_id,transaction.tax_amount,transaction.amount,transaction.amount-transaction.tax_amount as 'without_tax ',product_master.Product_name,product_master.vat from bill_master inner join transaction on bill_master.bill_no=transaction.bill_no inner join product_master on transaction.product_id=product_master.product_id where vat='14.50' and bill_master.bill_date=fd;
SET fd=DATE_ADD(fd,INTERVAL 1 DAY);
END WHILE;
END $$
DELIMITER;
calling statement:
CALL MonthlySalesReport('2016-03-06','2016-03-07');
error:
PROCEDURE aad_adr.MonthlySalesReport does not exist
Stored procedure create and drop statement have some problem, it have some space before name. Remove space and run command aad_adr.`
MonthlySalesReport(IN fromdate DATE, IN todate DATE)
Check space removed in below code.
DELIMITER $$
DROP PROCEDURE IF EXISTS `aad_adr`.`MonthlySalesReport` $$
CREATE PROCEDURE `aad_adr`.`MonthlySalesReport`(IN fromdate DATE,IN todate DATE)
BEGIN
Declare fd DATE;
Declare ed DATE;
SET fd=fromdate;
SET ed=todate;
WHILE DATE(fd)<=DATE(ed)DO
select bill_master.bill_no,DATE_FORMAT(bill_master.bill_date, '%y/%m/%d') AS 'formatted_date',transaction.product_id,transaction.tax_amount,transaction.amount,transaction.amount-transaction.tax_amount as 'without_tax ',product_master.Product_name,product_master.vat from bill_master inner join transaction on bill_master.bill_no=transaction.bill_no inner join product_master on transaction.product_id=product_master.product_id where vat='14.50' and bill_master.bill_date=fd;
SET fd=DATE_ADD(fd,INTERVAL 1 DAY);
END WHILE;
END $$
DELIMITER;

Select statement inside a loop in a Mysql Stored Procedure

Can we use Select statement inside a loop in a Mysql Stored Procedure?
why is the code wrong
create procedure AbsentReportproc (INOUT fromdate DATETIME, INOUT todate DATETIME)
as
begin
DECLARE startdate DATE;
DECLARE enddate DATE;
DECLARE nofdays INT;
DECLARE counter INT;
DECLARE countdate DATE;
startdate=fromdate;
enddate=todate;
countdate=fromdate;
nofdays=DATEDIFF(DAY,startdate,endate);
counter=1;
while counter<=noofdays
loop
select CARDNO from test_prefixmaster
where CARDNO not in ( select CARDNO from test_prefixtransactions where Date(S_DateTime)=countdate)
set countdate=countdate+1;
set counter=counter+1;
end loop;
end//
Try this:
DELIMITER $$
DROP PROCEDURE IF EXISTS `AbsentReportproc`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `AbsentReportproc`(IN _fromdate DATETIME, IN _todate DATETIME)
BEGIN
CREATE TEMPORARY TABLE daterange (dte DATE);
SET #counter := -1;
WHILE (#counter < DATEDIFF(DATE(_todate), DATE(_fromdate))) DO
INSERT daterange VALUES (DATE_ADD(_fromdate, INTERVAL #counter:=#counter + 1 DAY));
END WHILE;
SELECT tp.cardno, tp.EMPCODE, tp.DEPARTMENT, GROUP_CONCAT(d.dte) Absentddate, COUNT(tp.cardno) Totalnoofabsentdates
FROM test_prefixmaster tp JOIN daterange d
LEFT JOIN test_prefixtransactions tpt ON tp.cardno = tpt.CARDNO AND DATE(S_DateTime) = d.dte
WHERE tpt.CARDNO IS NULL
GROUP BY tp.cardno;
DROP TABLE daterange;
END$$
DELIMITER ;
You didn't need a loop for this. You can do so:
SELECT
p.CARDNO,
COUNT(CARDNO) AS countdate
FROM test_prefixmaster p
INNER JOIN test_prefixtransactions t ON p.CARDNO = t.CARDNO
WHERE Date(t.S_DateTime) BETWEEN fromdate
AND todate
GROUP BY p.CARDNO;

can't get MySQL query to work

I am trying to create a stored procedure on a Library database for practice mainly.
But it is confusing me slightly. I am using this query to create the procedure, and it says that it is working, and yet when I then call the procedure, it is saying that the procedure does not exist. Note: I am using phpmyadmin on a web database not on a local host.
delimiter $$
create procedure BorrowBook(in theBookID, in theUserID)
begin
declare lim int;
declare num int;
declare loanNumber int;
declare copyNumber int;
set lim = (select Readers.Limit from Readers where Readers.id = theUserID);
set num = (select Count(*) from Loans where Loans.UserID = theUserID);
set loanNumber = (select Count(*) from Loans) + 1;
set copyNumber = (select NumberAvailable from Book where Book.id = theBookID);
if(copyNumber > 0)
if(num<lim)
then
--Add a Loan to the Loans Table
insert into Loans values(loanNumber, theBookId, theUserID, curDate(), 0);
commit;
update Book set Book.NumberAvailable = Book.NumberAvailable - 1 where Book.id = theBookID;
select 'Succesful Update';
else
rollback;
select 'Borrow limit reached';
end if;
else
rollback;
select 'No copies available';
end;
delimiter ;
You are missing the $$ after the last end:
delimiter $$
...
end; $$ <-- add $$ here
delimiter ;
Shouldn't it be
end; $$
delimiter ;
instead of
end;
delimiter ;