phpmyadmin - Error in sql query - mysql

I have been trying to execute the following query without any luck. It says:
#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 '' at line 32
I am unable to point out any errors. Can anyone please tell me what am I doing wrong here?
DELIMITER $$
--
-- Procedures
--
CREATE PROCEDURE `Acc_Ledger`(IN `startDate` VARCHAR(200), IN `endDate` VARCHAR(200), IN `p_id` INT)
BEGIN
DECLARE RunningTotal, deb, cred DECIMAL(19,2);
DECLARE counter, row_count int;
SET RunningTotal = 0;
SET counter = 1;
DROP TABLE IF EXISTS LedgerTbl;
CREATE TEMPORARY TABLE LedgerTbl (CTR int primary key auto_increment, PARTY_ID INT,VRDATE VARCHAR(200),VRNOA VARCHAR(200),ETYPE VARCHAR(50),DESCRIPTION VARCHAR(500),DEBIT DECIMAL(19,2),CREDIT DECIMAL(19,2),RTotal DECIMAL(19,2));
INSERT INTO LedgerTbl
SELECT null, PARTY_ID,VRDATE,DCNO,ETYPE,DESCRIPTION,DEBIT,CREDIT, null FROM pledger WHERE PARTY_ID=p_id AND DATE(VRDATE) BETWEEN startDate AND endDate
ORDER BY VRDATE,ETYPE,DCNO;
SET #RunningTotal := 0;
SELECT SUM(DEBIT)-SUM(CREDIT) FROM pledger WHERE DATE(VRDATE) < startDate AND party_id = p_id INTO #RunningTotal;
IF ISNULL(#RunningTotal) THEN
SET #RunningTotal := 0;
END IF;
UPDATE LedgerTbl
SET RTotal = (#RunningTotal := #RunningTotal + (DEBIT - CREDIT));
SELECT * FROM LedgerTbl;
END$$

I missed
DELIMITER ;
at the end of query. Added this and everything is working fine.

Related

How to debug this MySQL stored procedure syntax error?

I have written a stored procedure in MySQL. I am getting issue in casting a day value from a date into integer value. I need to cast Day(now()) into integer, as I need to do some manipulation on this value.
Please see below for detail procedure code.
DROP PROCEDURE IF EXISTS `CREATE_PARTITION_FOR_MONTH`();
delimiter //
CREATE PROCEDURE `CREATE_PARTITION_FOR_MONTH`()
BEGIN
DECLARE current_month_total_days_count INT;
DECLARE current_month_days_left INT;
DECLARE current_day INT;
DECLARE next_month_first_date DATE ;
DECLARE loop_start_val INT DEFAULT 0;
DECLARE total_days_in_next_month INT;
DECLARE next_month_var_date DATE;
DECLARE partition_val VARCHAR(20) DEFAULT NULL;
DECLARE range_val INT DEFAULT 0;
SET current_month_total_days_count := CAST(DAY(LAST_DAY(now())) AS UNSIGNED);
SET current_day := CAST(EXTRACT(MONTH FROM CURDATE()) AS UNSIGNED);
SET current_month_days_left := current_month_total_days_count - current_day;
SET next_month_first_date := DATE(ADDDATE(CURDATE(),current_month_days_left));
SET total_days_in_next_month := DAY(LAST_DAY(next_month_first_date));
SET next_month_var_date := next_month_first_date;
SET loop_start_val := 0;
WHILE loop_start_val < total_days_in_next_month DO
SET next_month_var_date := DATE(ADDDATE(next_month_first_date,loop_start_val));
SET partition_val := CONCAT('p',TO_DAYS(next_month_var_date));
SET range_val := TO_DAYS(next_month_var_date);
ALTER TABLE XYZ ADD PARTITION (PARTITION partition_val VALUES LESS THAN (range_val));
SET loop_start_val := loop_start_val+1;
END WHILE;
END //;
DELIMITER ;
I am getting this error
ERROR 1064 (42000): 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 'range_val INT DEFAULT 0;
SET current_month_total_days_count = SELECT CAST(DAY(L' at line 11
Look at the line throwing error *DELCARE* range_val INT DEFAULT 0; ... DELCARE is the issue. Should be DECLARE. Funny ...

Mysql error nested arithmetic in select

When i am trying to execute this procedure it is showing an error.
USE `metro`;
DROP procedure IF EXISTS `transaction`;
DELIMITER $$
USE `metro`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `transaction`(
IN amount_p INT,
IN id_in INT,
IN id_out INT,
IN uidd INT
)
BEGIN
DECLARE uide INT;
DECLARE amt INT;
DECLARE NOW_AMT INT DEFAULT 0;
SELECT `amount` INTO amt FROM metro.wallet WHERE `uid`=uide;
INSERT INTO metro.transactions(`amount_deducted`,`time`,`station_id_in`,`station_id_out`,`uid`)
VALUES (amount_p,now(),id_in,id_out,uid);
SELECT uidd INTO uide;
SELECT uide;
SET NOW_AMT:=#amt+amount_p;
SELECT amt;
UPDATE metro.wallet SET `uid`=uide,`amount`=NOW_AMT;
END$$
DELIMITER ;
wallet is a table with uid and amount column.
Error code 1048, SQL state 23000: Column 'amount' cannot be null
Line 1, column 1
Execution finished after 0 s, 1 error(s) occurred.
Execution :
call metro.transaction(112,1,1,1);
Perhaps wrap your AMOUNT in a case statement where you can return a default if it is NULL.
SELECT case when `amount` is NULL then '' else `amount` END as `amount` INTO amt FROM metro.wallet WHERE `uid`=uide;

Error in MySQL 5.1 stored Procedure

I am new to stored procedure
DELIMITER //
CREATE PROCEDURE sp_MyNewTable
(IN Mod nvarchar(50),IN Did int,IN startdate datetime,IN enddate datetime)
BEGIN
Declare DateDuration int,
SET actstatus=1,
SET DateDuration = SELECT DATEDIFF(startdate,enddate) as Datediff
insert into mytable (Duration,Module,Deptid,taskstartdate,activestatus) values (DateDuration,Mod,did,enddate,startdate,actstatus)
Select * from mytable
END //
DELIMITER;
Getting error if I execute this:
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 sp_MyNewTable (IN Mod nvarchar(50),IN Did int,IN startdate
datetime,IN enddate datetime)
MYSQL version is MYSQL 5.1
Try:
DELIMITER //
CREATE PROCEDURE `sp_MyNewTable`(IN `Mod` VARCHAR(50),
IN `Did` INT,
IN `startdate` DATETIME,
IN `enddate` DATETIME)
BEGIN
DECLARE `DateDuration` INT;
DECLARE `actstatus` DATETIME;
SET `actstatus` := 1, `DateDuration` := DATEDIFF(`startdate`, `enddate`);
INSERT INTO `mytable`
(`Duration`, `Module`, `Deptid`, `taskenddate`, `taskstartdate`, `activestatus`)
VALUES
(`DateDuration`, `Mod`, `Did`, `enddate`, `startdate`, `actstatus`);
SELECT * FROM `mytable`;
END//
DELIMITER ;
SQL Fiddle demo

Convert MS-SQL Server stored procedure to MySQL Query

I am stuck at this for the past hour and unable to convert this MSSQL Server stored procedure to "MySQL query":
DECLARE #LedgerTbl TABLE (PARTY_ID VARCHAR(100),VRDATE VARCHAR(200),
VRNOA VARCHAR(200),ETYPE VARCHAR(50),
DESCRIPTION VARCHAR(500),DEBIT DECIMAL,
CREDIT DECIMAL, RunningTotal decimal)
DECLARE #RunningTotal decimal
SET #RunningTotal = 0
INSERT INTO #LedgerTbl
SELECT PARTY_ID,VRDATE,DCNO VRNOA,ETYPE,DESCRIPTION,DEBIT,CREDIT, null
FROM PLEDGER WHERE PARTY_ID=#partyId AND VRDATE BETWEEN #from AND #to
ORDER BY VRDATE,ETYPE,VRNOA
UPDATE #LedgerTbl
SET #RunningTotal = RunningTotal = #RunningTotal + (DEBIT-CREDIT)
FROM #LedgerTbl
SELECT * FROM #LedgerTbl
How may I convert this to a single MySQL query or a MySQL Stored procedure?
UPDATE
I tried to transform it to but it is giving me these error that I have given below:
DELIMETER //
CREATE PROCEDURE `Acc_Ledger` ()
BEGIN
DECLARE RunningTotal DECIMAL;
SET RunningTotal = 0;
CREATE TEMPORARY TABLE LedgerTbl (PARTY_ID VARCHAR(100),VRDATE VARCHAR(200),VRNOA VARCHAR(200),ETYPE VARCHAR(50),DESCRIPTION VARCHAR(500),DEBIT DECIMAL,RTotal decimal);
INSERT INTO LedgerTbl
SELECT PARTY_ID,VRDATE,DCNO VRNOA,ETYPE,DESCRIPTION,DEBIT,CREDIT, null
FROM PLEDGER WHERE PARTY_ID=17 AND VRDATE BETWEEN '2013/12/02' AND '2010/12/02'
ORDER BY VRDATE,ETYPE,VRNOA;
UPDATE LedgerTbl
SET RunninTotal = RTotal = RunningTotal + (DEBIT-CREDIT)
FROM LedgerTbl;
SELECT * FROM LedgerTbl;
END//
DELIMETER;
Here are the errors:
There seems to be an error in your SQL query. The MySQL server error output below, if there is any, may also help you in diagnosing the problem
ERROR: Unknown Punctuation String # 10 STR: // SQL: DELIMETER//
CREATE PROCEDURE Acc_Ledger () BEGIN
DECLARE RunningTotal DECIMAL;DELIMETER//
CREATE PROCEDURE Acc_Ledger () BEGIN
DECLARE RunningTotal DECIMAL;DELIMETER//
CREATE PROCEDURE Acc_Ledger () BEGIN
DECLARE RunningTotal DECIMAL;DELIMETER//
CREATE PROCEDURE Acc_Ledger () BEGIN
DECLARE RunningTotal DECIMAL;DELIMETER//
CREATE PROCEDURE Acc_Ledger () BEGIN
DECLARE RunningTotal DECIMAL;DELIMETER//
CREATE PROCEDURE Acc_Ledger () BEGIN
DECLARE RunningTotal DECIMAL;DELIMETER//
CREATE PROCEDURE Acc_Ledger () BEGIN
DECLARE RunningTotal DECIMAL;DELIMETER//
CREATE PROCEDURE Acc_Ledger () BEGIN
DECLARE RunningTotal DECIMAL;
SQL query:
DELIMETER// CREATE PROCEDURE Acc_Ledger () BEGIN DECLARE
RunningTotal DECIMAL;
MySQL said: Documentation
#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 'DELIMETER//
CREATE PROCEDURE Acc_Ledger () BEGIN
DECLARE RunningTotal' at line 1
Can anyone please review it?
It seems that the MSSQL procedure calculates a running total,
I've tested this procedure (slighly modified) on sqlfiddle and it gives the following results:
http://www.sqlfiddle.com/#!6/0e909/1
The MSSQL procedure contains probably a typo here: SELECT PARTY_ID,VRDATE,DCNO VRNOA,ETYPE, it's not clear whether DCNO VRNOA are two separate columns, or maybe one column DCNO_VRNOA with "missing" underscore between them.
I assume that they are two separate columns.
To calculate a running total in MySql there is no need to use a temporary table.
This simple query does this task:
SELECT PARTY_ID,VRDATE,DCNO, VRNOA,ETYPE,DESCRIPTION,
DEBIT,
CREDIT,
#RunningTotal := #RunningTotal + (DEBIT-CREDIT) RunningTotal
FROM PLEDGER ,
( SELECT #RunningTotal:=0) init_variables
WHERE PARTY_ID=1
AND VRDATE BETWEEN '2013-11-11' AND '2013-11-11'
ORDER BY VRDATE,ETYPE,VRNOA;
See demo here: --> http://www.sqlfiddle.com/#!2/daa6e/1
The procedure might look like:
DELIMITER /
CREATE PROCEDURE `Acc_Ledger` ()
BEGIN
SELECT PARTY_ID,VRDATE,DCNO, VRNOA,ETYPE,DESCRIPTION,
DEBIT,
CREDIT,
#RunningTotal := #RunningTotal + (DEBIT-CREDIT) RunningTotal
FROM PLEDGER ,
( SELECT #RunningTotal:=0) init_variables
WHERE PARTY_ID=1
AND VRDATE BETWEEN '2013-11-11' AND '2013-11-11'
ORDER BY VRDATE,ETYPE,VRNOA;
END /
DELIMITER ;
I had to write this procedure on my own, here is the link that helped me understand the Stored Procedures in MySQL: http://net.tutsplus.com/tutorials/an-introduction-to-stored-procedures/. And the procedure that I came up wit
DROP PROCEDURE IF EXISTS `Acc_Ledger`;
DELIMITER //
CREATE PROCEDURE `Acc_Ledger` ()
BEGIN
DECLARE RunningTotal, deb, cred DECIMAL(19,2);
DECLARE counter, row_count int;
SET RunningTotal = 0;
SET counter = 1;
DROP TABLE IF EXISTS LedgerTbl;
CREATE TEMPORARY TABLE LedgerTbl (CTR int primary key auto_increment, PARTY_ID INT,VRDATE VARCHAR(200),VRNOA VARCHAR(200),ETYPE VARCHAR(50),DESCRIPTION VARCHAR(500),DEBIT DECIMAL(19,2),CREDIT DECIMAL(19,2),RTotal DECIMAL(19,2));
INSERT INTO LedgerTbl
SELECT 0, PARTY_ID,VRDATE,DCNO,ETYPE,DESCRIPTION,DEBIT,CREDIT, null FROM PLEDGER WHERE PARTY_ID=17 AND VRDATE BETWEEN '2010/10/02' AND '2013/12/02'
ORDER BY VRDATE,ETYPE,DCNO;
SELECT COUNT(*) INTO row_count FROM LedgerTbl;
WHILE counter <= row_count DO
SELECT debit INTO deb FROM LedgerTbl WHERE ctr = counter;
SELECT credit INTO cred FROM LedgerTbl WHERE ctr = counter;
SET RunningTotal = RunningTotal + (deb-cred);
UPDATE LedgerTbl
SET LedgerTbl.RTotal = RunningTotal
WHERE ctr = counter;
SET counter = counter + 1;
END WHILE;
SELECT * FROM LedgerTbl;
END//
DELIMITER ;
UPDATE
Now I found the comparatively efficient solution:
DROP PROCEDURE IF EXISTS `Acc_Ledger`;
DELIMITER //
CREATE PROCEDURE `Acc_Ledger` ()
BEGIN
DECLARE RunningTotal, deb, cred DECIMAL(19,2);
DECLARE counter, row_count int;
SET RunningTotal = 0;
SET counter = 1;
DROP TABLE IF EXISTS LedgerTbl;
CREATE TEMPORARY TABLE LedgerTbl (CTR int primary key auto_increment, PARTY_ID INT,VRDATE VARCHAR(200),VRNOA VARCHAR(200),ETYPE VARCHAR(50),DESCRIPTION VARCHAR(500),DEBIT DECIMAL(19,2),CREDIT DECIMAL(19,2),RTotal DECIMAL(19,2));
INSERT INTO LedgerTbl
SELECT 0, PARTY_ID,VRDATE,DCNO,ETYPE,DESCRIPTION,DEBIT,CREDIT, null FROM PLEDGER WHERE PARTY_ID=17 AND VRDATE BETWEEN '2010/10/02' AND '2013/12/02'
ORDER BY VRDATE,ETYPE,DCNO;
SELECT COUNT(*) INTO row_count FROM LedgerTbl;
SET #RunningTotal := 0;
UPDATE LedgerTbl
SET RTotal = (#RunningTotal := #RunningTotal + (DEBIT - CREDIT));
SELECT * FROM LedgerTbl;
END//
DELIMITER ;

mysql truncating error

Create procedure a06_generate_data( p_loop_count int,
p_min int,
p_max int,
p_status char(1))
begin
declare v_max int;
declare v_min int;
declare v_loop_count int;
set v_max := p_max;
set v_min := p_min;
set v_loop_count := p_loop_count
-- clear the results table for each run
truncate table p_testbed.a06_rndData;
Repeat
insert into p_testbed.a06_rndData (col_value)
values (floor(v_min + rand()*(v_max – v_min)));
set v_loop_count := v_loop_count – 1;
until v_loop_count <= 0 End Repeat;
select col_value p_testbed.a06_rndData ;
end; #
This procedure is to insert random values starting from p_min to p_max with a total of p_loop_count values. But the problem is, there's an error around truncate:
ERROR 1064 (42000): 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 'truncate table p_testbed.a06_rndData;
Repeat
insert into p_testbed.a0'
I've searched online to check for the syntax for truncate, but I think I have this written correctly.
Suggestions? Thanks.
The problem is not with TRUNCATE TABLE, the line before is missing a semicolon:
set v_loop_count := p_loop_count