Alpesh Vasant 4/4/2024 11:28 AM • SET GLOBAL log_bin_trust_function_creators = 1;
4/4/2024 11:28 AM • Delimiter /
CREATE FUNCTION getmemberfirstname() RETURNS varchar(20)
BEGIN
RETURN(select count(*)mfname from member);
END /
Delimiter ;
Alpesh Vasant 4/4/2024 11:34 AM •
select getmemberfirstname();
4/4/2024 11:37 AM • Delimiter @
create function malemembers()
returns int
begin
RETURN(select count(*) from member where sex = 'male') ;
end @
Delimiter ;
select malemembers();
Alpesh Vasant 4/4/2024 11:50 AM • -----------------------------
Delimiter #
CREATE FUNCTION getAuthorCountryCount(country varchar(20)) returns int
Begin
RETURN(select count(*) from author where nationality = country) ;
End #
Delimiter ;
SELECT getAuthorCountryCount('INDIA');
SELECT getAuthorCountryCount('usa');
Alpesh Vasant 4/4/2024 11:58 AM • delimiter %
create function getmembercountbysex(gender varchar(10)) returns int
begin
DECLARE cnt int(10) ;
SELECT COUNT(*) FROM MEMBER where sex = gender INTO cnt ;
RETURN(cnt);
end %
delimiter ;
select getmembercountbysex('female');
select getmembercountbysex('male');
240350320019_Ganesh 4/4/2024 12:15 PM • sir here why we are writting varchar(10)
getmembercountbysex(gender varchar(10))
returns int
Alpesh Vasant 4/4/2024 12:38 PM • delimiter &
create function getAuthorAge(aname varchar(30)) RETURNS int
begin
declare agg int(10);
select age INTO agg from author where authorname = aname;
return (agg);
end &
delimiger ;
select getAuthorAge('Robert Kiyosaki');
--> Use of function in select query => select authorname,getAuthorAge(authorname)
from author;
-------------------------------------------------------------------------
Alpesh Vasant 4/4/2024 1:06 PM •
-------------------------------------------------------------------------
delimiter ^
create function checkgratuity(dateoj date) returns varchar(50)
begin
declare msg varchar(50);
IF year(current_date)-year(dateoj) >= 5 THEN SET msg = ' Eligible for Gratuity' ;
ELSE SET msg = ' NOT Eligible for Gratuity' ;
END IF;
return (msg);
end ^
delimiter ;
select checkgratuity('2015-09-01');
select checkgratuity('2021-09-01');
select checkgratuity('2019-09-01');
select checkgratuity('2020-09-01');
select efname, doj, checkgratuity(doj) from employee;
--------------------------------------------------
write a function to calculate the bonus for employees
as follows.....
salary < 10000 --> bonus 20%
salary >= 10000 and < 20000 --> bonus 10%
salary >= 20000 --> bonus 5%
Alpesh Vasant 4/4/2024 1:16 PM • PROCEDURE
--------------------------------
delimiter //
CREATE PROCEDURE getDeptInfo()
begin
select * from department;
end //
delimiter ;
CALL getDeptInfo;
Alpesh Vasant 4/4/2024 1:24 PM •
-------------------------------------------------------------------------
delimiter @@
create procedure getIndiaAuthorInfo()
begin
select * from author where nationality = 'India';
end @@
delimiter ;
CALL getIndiaAuthorInfo;
Alpesh Vasant 4/5/2024 10:22 AM • SELECT ROUTINE_NAME,ROUTINE_TYPE FROM
INFORMATION_SCHEMA.ROUTINES where ROUTINE_TYPE = 'FUNCTION' AND ROUTINE_SCHEMA =
'hyblms';
Alpesh Vasant 4/5/2024 10:45 AM • delimiter /
create procedure extwoprocedures()
begin
CALL getDeptInfo;
CALL getIndiaAuthorInfo;
end /
delimiter ;
Alpesh Vasant 4/5/2024 11:01 AM • delimiter #
create procedure getselectedbooks(IN cat varchar(20), IN pages int(20))
begin
SELECT bookid, bookname, catogery,noofpages from book where catogery = cat AND
noofpages < pages;
end #
delimiter ;
call getselectedbooks('Fiction',400) ;
call getselectedbooks('Fiction',300) ;
call getselectedbooks('Fiction',200) ;
4/5/2024 11:14 AM • passing an input and capturing output in a OUT variable
--------------------------------------------------------------------
delimiter #
create procedure getselectedbookname(IN bid varchar(20), OUT bname varchar(30))
begin
SELECT bookname from book where bookid = bid INTO bname ;
end #
delimiter ;
call getselectedbookname('B009',@bkname);
select @bkname;
call getselectedbookname('B007',@bkname);
select @bkname;
Alpesh Vasant 4/5/2024 11:26 AM •
-----------------------------------------------------------------------
delimiter /
create procedure countstatus(OUT activecnt int(10), OUT notactivecnt int(10))
begin
select count(*) INTO activecnt from member where status = 'active';
select count(*) INTO notactivecnt from member where status = 'not active';
end /
delimiter ;
select @acnt;
select @nacnt;
select concat('Active Members Count: ',@acnt);
select concat('Not Active Members Count: ',@acnt) as NotActiveCount;
select @acnt as ActiveCount, @nacnt as NotActiveCount;
----------------------------------------------------------------------------------
Alpesh Vasant 4/5/2024 11:51 AM •
----------------------------------------------------------------------------------
delimiter #
create procedure getEmpStatus(IN eid varchar(10), OUT workyears int(10), OUT
empStatus varchar(50))
begin
declare joindate date;
select doj from employee where employeeid = eid INTO joindate;
select checkgratuity(joindate) INTO empStatus;
select year(current_date)-year(doj) workyears from employee where EmployeeId = eid
INTO workyears;
end #
delimiter ;
call getEmpStatus('E004',@yrs, @sts);
select @yrs , @sts;
call getEmpStatus('E001',@yrs, @sts);
select @yrs , @sts;
-----------------------------------------------------------------------
Alpesh Vasant 4/5/2024 12:31 PM •
-----------------------------------------------------------------------
year of doj , 2024 --> latest member
year of doj , 2023 --> last year member
year of doj , <2023 --> old member
delimiter /
create procedure getMemberStatus(IN mbrid varchar(10), out joindate date, OUT
mbrsts varchar(70))
begin
declare yr int(10);
select year(doj) , doj from member where memberid = mbrid INTO yr , joindate;
CASE
WHEN yr = 2024 THEN SET mbrsts = ' latest member';
WHEN yr = 2023 THEN SET mbrsts = ' last year member';
WHEN yr < 2023 THEN SET mbrsts = ' old member';
ELSE
SET mbrsts = 'Sorry....Member does not present in our list' ;
END CASE;
end /
delimiter ;
call getMemberStatus('M008',@dt , @msts);
select @dt , @msts;
call getMemberStatus('M009',@dt , @msts);
select @dt , @msts;
call getMemberStatus('M908',@dt , @msts);
select @dt , @msts;
Alpesh Vasant 4/5/2024 1:01 PM •
--------------------------------------------------------------------------
INOUT TYPE OF variable in procedure
---------------------------------------------
delimiter /
create procedure updatesalary(IN eid varchar(10), INOUT sal int(30))
begin
update employee set salary = sal + sal*0.1 where employeeid = eid ;
select salary from employee where employeeid = eid INTO sal;
end /
delimiter ;
select salary from employee where employeeid = 'E001' INTO @sal;
call updateSalary('E001' , @sal,);
select @sal;
select salary from employee where employeeid = 'E003' INTO @sal;
call updateSalary('E003', @sal);
select @sal;
-----------------------------------------------------------------------------------
------------
Alpesh Vasant 4/5/2024 1:01 PM •
--------------------------------------------------------------------------
INOUT TYPE OF variable in procedure
---------------------------------------------
delimiter /
create procedure updatesalary(IN eid varchar(10), INOUT sal int(30))
begin
update employee set salary = sal + sal*0.1 where employeeid = eid ;
select salary from employee where employeeid = eid INTO sal;
end /
delimiter ;
select salary from employee where employeeid = 'E001' INTO @sal;
call updateSalary('E001' , @sal,);
select @sal;
select salary from employee where employeeid = 'E003' INTO @sal;
call updateSalary('E003', @sal);
select @sal;
-----------------------------------------------------------------------------------
------------
4/5/2024 1:25 PM • delimiter /
create procedure insertproduct(IN pid varchar(10), IN pname varchar(30), IN pqty
int(20))
begin
DECLARE EXIT HANDLER FOR 1048
Begin
Select CONCAT("Sorry....Product id can not have NULL value") as message;
End ;
DECLARE EXIT HANDLER FOR 1062
Begin
Select CONCAT(pid , " is already present in table with different product") as
message;
End ;
insert into product values (pid , pname, pqty);
select * from product;
end /
delimiter ;
4/5/2024 1:26 PM • call insertproduct('P002', 'abc',500);
4/5/2024 1:26 PM • call insertproduct(NULL, 'abc',500);