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 ;
Related
I have the fallowing procedure:
DELIMITER $$
CREATE OR REPLACE PROCEDURE add_comments
AS
BEGIN
DECLARE var INT;
SELECT grade INTO var FROM table1;
IF (var <= 7) THEN
UPDATE table2 set comment = not_good;
ELSE
UPDATE table2 set comment = good;
END IF;
END$$
comment, not_good and good are all columns from table2
but it gives me the error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'AS
BEGIN
IF (var <= 7)
BEGIN
UPDATE table2 set comment ' at line 3
Can't find out the problem, can someone help me with this?
Thanks!
Replace
DELIMITER $$
CREATE OR REPLACE PROCEDURE add_comments
AS
BEGIN
With
DELIMITER $$
CREATE OR REPLACE PROCEDURE add_comments()
BEGIN
PS: add brackets and remove "AS"
Remove that AS operator from your procedure code body. Your procedure should look like
DELIMITER $$
CREATE OR REPLACE PROCEDURE add_comments
BEGIN
DECLARE var INT;
SELECT grade INTO var FROM table1;
IF (var <= 7) THEN
UPDATE table2 set comment = not_good;
ELSE
UPDATE table2 set comment = good;
END IF;
END$$
Refer MySQL Documentation for more information
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;
I have a MySQL stored procedure that throws an error.
DELIMITER $$
DROP PROCEDURE IF EXISTS `test_schema`.`TEST_SPROC` $$
CREATE PROCEDURE `test_schema`.`TEST_SPROC` (IN var0 INT, var1 INT)
BEGIN
DECLARE var0 INT;
DECLARE var1 INT;
SELECT COUNT(*) INTO var0 FROM INFORMATION_SCHEMA.`TABLES`
WHERE `TABLE_SCHEMA`='test_schema' AND `TABLE_NAME`='original_table';
SELECT COUNT(*) INTO var1 FROM INFORMATION_SCHEMA.`COLUMNS`
WHERE `TABLE_SCHEMA`='test_schema' AND `TABLE_NAME`='new_table'
AND `COLUMN_NAME`='id';
IF var0=1 THEN
RENAME TABLE test_schema.original_table TO test_schema.new_table;
END IF;
IF var1=1 THEN
ALTER TABLE test_schema.new_table CHANGE id AccountID VARCHAR;
END IF; #error is thrown here.
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 '; END IF;
END' at line 15
What is wrong with my if statement?
Change this line:
ALTER TABLE test_schema.new_table CHANGE id AccountID VARCHAR;
to this:
ALTER TABLE test_schema.new_table CHANGE id AccountID VARCHAR(100);
Of course you should specify a length for the VARCHAR column that is appropriate. I've just used VARCHAR(100) as an example.
You have to specify a length for the alter table statement. See the comment below:
IF varTable=1 THEN
RENAME TABLE test_schema.original_table TO test_schema.new_table;
SELECT COUNT(*) INTO varColumn FROM INFORMATION_SCHEMA.`COLUMNS`
WHERE `TABLE_SCHEMA`='test_schema' AND `TABLE_NAME`='new_table'
AND `COLUMN_NAME`='id';
IF varColumn=1 THEN
#The following statement must be "VARCHAR(255)" not just "VARCHAR"
ALTER TABLE test_schema.new_table CHANGE id AccountID VARCHAR(255);
ELSE
#statements.
END IF;
END IF;
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;
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 ;