Error in MySQL 5.1 stored Procedure - mysql

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

Related

Declaring INT variable to return in MYSQL Function problem

I'm trying to build a mysql function which inserts values into a table and returns me the last_id inserted, which should be the same after executing the insertion. But Xampp gives me an error in the line where im declaring the "last_bill_id" variable. Can someone please help me to understand what am I doing wrong?
Here is the code for the function:
CREATE FUNCTION insert_bill(
client_id varchar (12),
bill_date date
) RETURNS INT
BEGIN
DECLARE last_bill INT;
INSERT INTO bill
(
client_id, bill_date
)
VALUES
(
client_id, bill_date
);
SET last_bill = LAST_INSERT_ID();
RETURN last_bill;
END $$
DELIMITER ;
Error: #1064 - Something is wrong about 'INT
you need another DELIMITER at the start
DELiMiTER $$
CREATE FUNCTION insert_bill(
client_id varchar (12),
bill_date date
) RETURNS INT
DETERMINISTiC
BEGIN
DECLARE last_bill INT;
INSERT INTO bill
(
client_id, bill_date
)
VALUES
(
client_id, bill_date
);
SET last_bill := LAST_INSERT_ID();
RETURN last_bill;
END $$
DELIMITER ;

phpmyadmin wamp mysql cannot declare variable in procedure

I have unsloved problem when I try to create a stored procedure following code below
but it shows error
#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 5
and this is my code Thanks for advice.
CREATE DEFINER=root#localhost PROCEDURE item_page_insert(IN cid INT, IN it_title VARCHAR(200), IN tumbnail VARCHAR(300), IN publish_date DATE, IN cover_set VARCHAR(20), IN pcontent TEXT, IN status INT) MODIFIES SQL DATA
BEGIN
DECLARE mpid INT;
SELECT max(pid)+1 INTO mpid
FROM tbpage;
INSERT INTO tbite(cid, pid, it_title, tumbnail, publish_date, cover_set)
VALUES(cid,
mpid,
it_title,
tumbnail,
publish_date,
cover_set);
INSERT INTO tbpage(pid, pcontent, set_date, status)
VALUES(mpid,
pcontent,
now(),
status); END;
DELIMITER ;
it should be
DECLARE mpid AS INT;
try that one

phpmyadmin - Error in sql query

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.

if ... then stored procedure

I have an error in this stored procedure:
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 'INSERT INTO escuelas_asignacion_maestro (idEscuela,
idCiclo, `idGrado' at line 31
Code:
DELIMITER $$
DROP PROCEDURE IF EXISTS `zz73ff`.`pr_trasladar_asignaciones_por_ciclo` $$
CREATE PROCEDURE `zz73ff`.`pr_trasladar_asignaciones_por_ciclo`
(IN p_ciclo int, IN n_ciclo int)
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE escuela, ciclo, grado, nGrupos, maestro INT;
DECLARE fecha datetime;
DECLARE cursor_asignaciones CURSOR FOR
SELECT idEscuela, idCiclo, idGrado, numeroGrupos, idMaestro FROM escuelas_asignacion_maestro
WHERE idCiclo = p_ciclo
AND idEstatus = 1
ORDER BY idEscuela, idGrado;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
SELECT now() INTO fecha;
OPEN cursor_asignaciones;
REPEAT
FETCH cursor_asignaciones INTO escuela, ciclo, grado, nGrupos, maestro;
IF grado > 1 THEN
SELECT idEscuela, idCiclo, idGrado, numeroGrupos, idMaestro INTO
escuela, ciclo, grado, nGrupos, maestro
FROM escuelas_asignacion_maestro
WHERE idCiclo = p_ciclo AND idEstatus = 1 AND idEscuela = escuela
AND idGrado = (grado - 1);
END IF
INSERT INTO `escuelas_asignacion_maestro`
(`idEscuela`, `idCiclo`, `idGrado`, `numeroGrupos`, `idEstatus`, `idMaestro`, `fechaModificacion`)
VALUES (escuela, n_ciclo, grado, nGrupos, 1,0, fecha);
UNTIL done END REPEAT;
CLOSE cursor_asignaciones;
END $$
DELIMITER ;

Conversion ms sql function into mysql

I'm facing a problem while converting ms sql function into mysql. Here is ms sql function code:
CREATE FUNCTION [crewu2].[isAvailable] (#OwnerID int, #DateFrom Smalldatetime, #DateTo Smalldatetime)
RETURNS bit AS
BEGIN
DECLARE #t bit
IF #DateFrom IS NULL or #DateTo IS NULL
or EXISTS (select DateID
from [client_BlackDates]
where OwnerID=#OwnerID
and ((DateFrom<=#DateFrom and #DateFrom<=DateTo)
or (DateFrom<=#DateTo and #DateTo<=DateTo)))
SET #t=0
ELSE
SET #t=1
RETURN #t
END
And here is converted in mysql:
DELIMITER $$
CREATE FUNCTION isAvailable (OwnerID INT, DateFrom DATETIME, DateTo DATETIME)
RETURNS BIT
BEGIN
DECLARE t BIT;
IF DateFrom IS NULL OR DateTo IS NULL
OR EXISTS (SELECT DateID
FROM client_BlackDates
WHERE OwnerID=OwnerID
AND ((DateFrom<=DateFrom AND DateFrom<=DateTo)
OR (DateFrom<=DateTo AND DateTo<=DateTo)))
SET t=0;
ELSE
SET t=1;
RETURN t
END $$
DELIMITER;
but it gives me following error:
Error Code : 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 'SET t=0;
ELSE
SET t=1;
RETURN t
END' at line 6
Please anyone help me regarding this.
This should work(You have missed THEN and END IF):
DELIMITER $$
CREATE FUNCTION isAvailable (OwnerID INT, DateFrom DATETIME, DateTo DATETIME)
RETURNS BIT
BEGIN
DECLARE t BIT;
IF DateFrom IS NULL OR DateTo IS NULL OR EXISTS (SELECT DateID FROM client_BlackDates WHERE OwnerID=OwnerID AND ((DateFrom<=DateFrom AND DateFrom<=DateTo) OR (DateFrom<=DateTo AND DateTo<=DateTo)))
**THEN**
SET t=0;
ELSE
SET t=1;
RETURN t;
**END IF;**
END $$
DELIMITER ;
Refer MySQL docs.
Take a look into MySQL manual to the IF..THEN..ELSE sintax
DELIMITER //
CREATE FUNCTION isAvailable (OwnerID INT, DateFrom DATETIME, DateTo DATETIME)
RETURNS BIT
BEGIN
DECLARE t BIT;
IF DateFrom IS NULL OR DateTo IS NULL
OR EXISTS (SELECT DateID
FROM client_BlackDates
WHERE OwnerID=OwnerID
AND ((DateFrom<=DateFrom AND DateFrom<=DateTo)
OR (DateFrom<=DateTo AND DateTo<=DateTo)))
THEN
SET t = 0;
ELSE
SET t = 1;
END IF;
RETURN t;
END //
DELIMITER ;