Create function on mysql - mysql

I have the following on mysql:
DELIMITER //
CREATE FUNCTION dateDiff1(contract_id INT, cust_id INT)
RETURNS INT
BEGIN
DECLARE startDate, endDate DATETIME;
DECLARE result int;
SET startDate = (SELECT startDate FROM contract WHERE insurance_cover_id = contract_id AND customer_id = cust_id);
SET endDate = (SELECT endDate FROM contract WHERE insurance_cover_id = contract_id AND customer_id = cust_id);
SET result = (SELECT TIMESTAMPDIFF(MONTH, endDate, startDate));
RETURN result;
END;
//
DELIMITER ;
SELECT dateDiff1(1,1);
and it returns NULL, any suggestion?

Maybe, you could solved your issue as follows as far as you got some data in your database:
CREATE FUNCTION datediff1(_contract_id INT [, _cust_id INT])
RETURNS INT DETERMINISTIC
BEGIN
DECLARE res INT;
SELECT TIMESTAMPDIFF(MONTH, endDate, startDate) FROM contract WHERE contract_id =
_contract_id [AND cust_id = _cust_id] INTO res;
RETURN res;
END
I marked the cust_id related parts as optional since it is most likely that you contract_id is already referred to a particular customer.
I hope it will help. Good luck.

Related

Mysql Decimal division automatically round up

My Function is
DELIMITER $$;
CREATE FUNCTION get_approved_leaves(f_date date, t_date date,
emp_id bigint, ltp_id int)
RETURNS INT NOT DETERMINISTIC
BEGIN
DECLARE leaves_taken decimal(10,3) DEFAULT 0.000;
SELECT SUM(eld_first+eld_second)/2
INTO leaves_taken
FROM erp_apply_leaves, erp_employee_leave_debts
WHERE al_fk_employees = emp_id
AND al_from >= f_date
AND al_to <= t_date
AND al_fk_leave_type = ltp_id
AND eld_fk_apply_leaves = al_id
AND al_status = 101;
RETURN leaves_taken;
END $$;
DELIMITER ;
When I call
SELECT get_approved_leaves('2017-01-01','2017-12-31',18, 5) as debted
FROM `erp_employee_leave_debts` WHERE 1
It gives the out put : 20
But here SUM(eld_first+eld_second) = 39. So I need to get the output = 19.5
Your function definition is expecting an INT as return value.
And as you are returning leaves_taken value, which is of type DECIMAL(10,3),
SQL Engine is converting it to nearest INTEGER value, to match the return type, and returning.
If you want to keep the original value, better you change the return type of the function.
Example is given below:
CREATE FUNCTION
get_approved_leaves( f_date date, t_date date
, emp_id bigint, ltp_id int )
RETURNS DECIMAL(10,3) NOT DETERMINISTIC

MySQL stored procedure is not working

I am new to Mysql. I have a stored procedure,below is my SP
CREATE DEFINER=`root`#`localhost` PROCEDURE `insertGroup`(IN startdate date,
IN enddate date,
IN amount int,
IN groupname char(50))
BEGIN
DECLARE c_id INT;
set c_id =(select id from c_begin where startdate = startdate and enddate = enddate and amount = amount);
insert into c_group (c_beginid,Groupname)
select * from (select c_id,groupname) as temp
where not exists (
select c_beginid,groupname from c_group where c_beginid = c_id and groupname = groupname
) ;
END
I am calling it like
call insertGroup ('2014-01-01','2015-12-31',100000,'GroupD');
But it is not inserting any row. I am trying to insert into "c_group" if the c_beginid and groupname is not exists.
But when i try like below
insert into c_group (c_beginid,Groupname)
select * from (select 1,'GroupD') as temp
where not exists (
select c_beginid,groupname from c_group where c_beginid = 1 and groupname = 'GroupD'
) limit 1 ;
it is working.
So What went wrong in my "insertGroup" SP. Can any one help me ?
Thanks ,
Rename your input parameter to make it clear to the DB engine when you mean the parameter and when the column name.
where startdate = startdate and enddate = enddate and amount = amount
is always true since the engine thinks you compare a column to itself.

Mysql Table function and view returns null value

I really don't know why my function and view in phpmyadmin returns null instead of DATETIME.
This is my function:
DELIMITER $$
CREATE FUNCTION `return_file`( name varchar(10), number int )
RETURNS datetime
DETERMINISTIC
BEGIN DECLARE transdate DATETIME;
SET trans_date = (SELECT `date` from `sample_table` WHERE `name_ref` = name and `ref_number` = number and `type` = 10);
RETURN trans_date;
END$$
DELIMITER ;
While this is my view:
CREATE VIEW getsME as
SELECT id,type,date,name_ref,ref_number,return_file(name_ref,ref_number) as transDate
FROM sample_table
WHERE type= 11
ORDER BY id ASC

A sql function that returns true/false with some data calculations problems

I need to create a function in sql that returns 0 or 1 if the "totalOrderSum" is lower than 1000 or higher. The parameters that are going in are #artNr, #orderNr and #amount. The problem is that I have to find the #artNr price from another table and get that price * #amount and after that I have to get the total TotalAmount from one more table and see if (TotalAmount + (#price * #amount) < 1000).
I have some sql code below and I think you know what i want to do with the code.
Many thanks in advance.
CREATE FUNCTION Lab2_spAddOrderLine(#artNr INT,#orderNr INT,#amount) RETURNS BIT
WITH SCHEMABINDING
AS BEGIN
DECLERE #result BIT, #price INT,#totalPrice
IF(
SELECT Price
FROM Lab2_Article
WHERE ArtNr=#artNr AS #price
AND WHERE (
SELECT TotalAmount
FROM Lab2_CostomOrder
WHERE OrderNr = #orderNr AS #totalPrice
AND WHERE ((#totalPrice +(#price * #amount)) < 1000
)
)
SET #result = 1
ELSE
SET #result = 0
RETURN #result
END
DELIMITER //
CREATE PROCEDURE lab2_addOrderLineProcedure
(
IN articleNr INT,
IN orderNr INT,
IN amount INT,
OUT result BIT
)
BEGIN
DECLARE price INT;
DECLARE totalPrice INT;
SET price = (SELECT Price FROM Lab2_Article WHERE ArtNr = articleNr);
SET totalPrice = ((SELECT TotalPrice FROM Lab2_CostomOrder WHERE orderNr = orderNr) + (price * amount));
IF totalPrice < 1000 THEN
INSERT INTO Lab2_OrderLine VALUES (articleNr, orderNr, amount);
SET result = 1;
ELSE
SET result = 0;
END IF;
END //
DELIMITER ;
There are so many errors in your code. Try the following:-
CREATE FUNCTION Lab2_spAddOrderLine(`artNr` INT, `orderNr` INT, `amount` int)
RETURNS BIT
BEGIN
DECLARE `result` BIT, `price` INT, `totalPrice` INT
IF(
SELECT Price
FROM Lab2_Article
WHERE ArtNr=`artNr` AS #price
AND (
SELECT TotalAmount
FROM Lab2_CostomOrder
WHERE OrderNr = `orderNr` AS `totalPrice`
AND ((`totalPrice` +(`price` * `amount`)) < 1000
)
)
SET `result` = 1
ELSE
SET `result` = 0
RETURN `result`
END;

Mysql Function returns Null always

I have a function in mysql like below:
DELIMITER $$
CREATE DEFINER=root#localhost FUNCTION fnGetDropDownValue(
itemValue varchar(300),
DropDownId int,
CId int
) RETURNS int(11)
BEGIN
DECLARE listId int;
SELECT ListID into listId FROM DropDownListValues WHERE LOWER(ListValue) = LOWER(itemValue) AND DropDownListID = DropDownId AND (ClientId = 0 OR ClientId = CId);
RETURN listId;
END$$
But it always returns Null values when I use
SELECT fnGetDropDownValue('General', 24, 18);
I don't know what I am doing wrong :(
After having the case sensitive issue with mysql columns I used to have variable names to start with _ to avoid it messing with column names. Now the stored procedure looks like this:
DELIMITER $$
CREATE DEFINER=root#localhost FUNCTION fnGetDropDownValue(
itemValue varchar(300),
DropDownId int,
CId int
) RETURNS int(11)
BEGIN
DECLARE _listId int;
SELECT ListID into _listId FROM DropDownListValues WHERE LOWER(ListValue) = LOWER(itemValue)
AND DropDownListID = DropDownId AND (ClientId = 0 OR ClientId = CId);
RETURN _listId;
END$$
This way it will work on any platform and it may be useful for others.