Conversion ms sql function into mysql - 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 ;

Related

MySQL Error 1064 when create function

When I am trying to create a function in mysql 5.7.20, I met error 1064:
delimiter //
create function add_favorstocks (
uid_int INT,
stockid_char CHAR(20),
added_date CHAR(20)
)
returns INT
begin
declare ret INT;
case
when exists (select 1 from user_favorstocks where uid=uid_int and stockid = stockid_char)
then begin
insert into user_favorstocks (uid, stockid, added_date) values (uid_int, stockid_char, added_date);
set ret=1;
end
else
begin
case (select is_deleted from user_favorstocks where uid=uid_int and stockid = stockid_char)
when 0 then set ret=0;
else begin
update user_favorstocks set is_deleted=0, db_update_time=now() where uid=uid_int and stockid=stockid_char;
set ret=3;
end;
end
end
end
return ret
end//
delimiter ;
The error message is
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 'else
begin
case (select is_deleted from user_favorstocks whe' at line 15
Does this because that I am using begin...end in an else clause?
I found the example in MySQL's documentation:
DELIMITER |
CREATE PROCEDURE p()
BEGIN
DECLARE v INT DEFAULT 1;
CASE v
WHEN 2 THEN SELECT v;
WHEN 3 THEN SELECT 0;
ELSE
BEGIN
END;
END CASE;
END;
|
It seems valid to use a begin...end statement in an else clause.
Try:
DROP FUNCTION IF EXISTS `add_favorstocks`;
delimiter //
create function add_favorstocks (
uid_int INT,
stockid_char CHAR(20),
added_date CHAR(20)
)
returns INT
begin
declare ret INT;
case when exists (select 1
from user_favorstocks
where uid=uid_int and
stockid = stockid_char)
then begin
insert into user_favorstocks
(uid, stockid, added_date)
values
(uid_int, stockid_char, added_date);
set ret=1;
-- end <-- missing a semicolon
end;
else
begin
case (select is_deleted
from user_favorstocks
where uid=uid_int and
stockid = stockid_char)
when 0 then set ret=0;
else begin
update user_favorstocks
set is_deleted=0,
db_update_time=now()
where uid=uid_int and
stockid=stockid_char;
set ret=3;
end;
-- end <-- missing CASE and a semicolon
end case;
-- end <-- missing a semicolon
end;
-- end <-- missing CASE and a semicolon
end case;
-- return ret <-- missing a semicolon
return ret;
end//
delimiter ;

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

User Defined Function MySQL Workbench

How do I execute a user defined function so that the database will recognize that the function has been created?
DELIMITER $$
CREATE FUNCTION `StudentGPA` (StudentID int, ClassStartDateStart datetime, ClassStartDateEnd date time) RETURNS decimal(3,2)
BEGIN
Declare AvgGPA decimal(3,2);
SELECT AvgGPA= isNULL(Avg(GPA), 0.0)
FROM Students_Classes
WHERE StudentID = StudentID
AND StartDate > ClassStartDateStart
AND StartDate <= ClassStartDateEnd
AND GPA > 0; RETURN AvgGPA;
END
When running your code, you should get a syntax error. In your CREATE statement where you're specifying your parameters you need to change ClassStartDateEnd date time to ClassStartDateEnd datetime.
DELIMITER $$
CREATE FUNCTION `StudentGPA`
(StudentID int,
ClassStartDateStart datetime,
ClassStartDateEnd datetime)
RETURNS decimal(3,2)
BEGIN
Declare AvgGPA decimal(3,2);
SELECT AvgGPA= isNULL(Avg(GPA), 0.0)
FROM Students_Classes
WHERE StudentID = StudentID
AND StartDate > ClassStartDateStart
AND StartDate <= ClassStartDateEnd
AND GPA > 0; RETURN AvgGPA;
END$$
DELIMITER ;
I have also used your DELIMITER of $$ after your END statement to properly terminate the CREATE, then set the delimiter back to ; which is good practice.

Getting a MYSQL 1064 error when creating a function

DELIMITER $$
CREATE FUNCTION nameOfFunct(intIn int)
RETURN int
BEGIN
DECLARE intOut INT;
SET intOut = SELECT count(*)
FROM tableToTakeFrom
WHERE columToCompareTo = intIn;
RETURN intOut;
END;
$$
DELIMITER;
If I try to run this all I get is:
SQL 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
'RETURN int
BEGIN
DECLARE intOut INT;
SET intOut = select count(' at line 2
A couple of changes to resolve the problem:
DELIMITER $$
CREATE FUNCTION nameOfFunct(intIn INT)
-- RETURN INT
RETURNS INT
BEGIN
DECLARE intOut INT;
/*SET intOut = SELECT COUNT(*)
FROM tableToTakeFrom
WHERE columToCompareTo = intIn;*/
SET intOut = (SELECT COUNT(*)
FROM tableToTakeFrom
WHERE columToCompareTo = intIn);
RETURN intOut;
END $$
DELIMITER ;
Defining the return type is done by the RETURNS keywrod, not the RETURN keyword:
CREATE FUNCTION nameOfFunct(intIn int)
RETURNS int
BEGIN
DECLARE intOut INT;
SET intOut = SELECT count(*)
FROM tableToTakeFrom
WHERE columToCompareTo = intIn;
RETURN intOut;
END;

Error in Sql Query - Can't find the solution

I'm having big troubles to find the error in my sql query.
CREATE FUNCTION freeSeats(bookingID INT)
RETURNS VARCHAR(30)
BEGIN
DECLARE numberBooked INT ;
DECLARE status VARCHAR(30);
SELECT count(*) FROM passenger WHERE Booking IN(SELECT Id FROM booking WHERE
Flight = (SELECT Flight FROM booking WHERE Id = bookingID)) INTO numberBooked;
IF (numberBooked > 59) THEN SET status =”No free seats”;
ELSE SET status =”OK”;
END IF;
RETURN status;
END;
I get this error message:
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 'free seats”; ELSE SET status =”OK”; END IF;
RETURN status; END' at line 10
I would appreciate some help.
Thanks in advance
don't for get to change the DELIMITER
use single quotes
example,
DELIMITER $$
CREATE FUNCTION freeSeats(bookingID INT)
RETURNS VARCHAR(30)
BEGIN
DECLARE numberBooked INT ;
DECLARE status VARCHAR(30);
SET numberBooked =
(
SELECT count(*)
FROM passenger
WHERE Booking IN
( SELECT Id
FROM booking
WHERE Flight = (SELECT Flight FROM booking WHERE Id = bookingID)
)
);
IF (numberBooked > 59) THEN
SET status = 'No free seats';
ELSE
SET status = 'OK';
END IF;
RETURN status;
END $$
DELIMITER ;