0% found this document useful (0 votes)
24 views

MySQL Stored Procedure Codes

Uploaded by

Neha Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

MySQL Stored Procedure Codes

Uploaded by

Neha Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

1.

Error Handling

CREATE DEFINER=`root`@`localhost` PROCEDURE `Insert_Emp_EH`( p_EmpID INT, p_EmpName


VARCHAR(100), p_EmailAddress VARCHAR(100))
BEGIN
DECLARE exit handler for 1062
BEGIN
-- Error occurred
SELECT 'Error occurred' AS Message;
END;

INSERT INTO Emp_EH (EmpID, EmpName, EmailAddress)


VALUES (p_EmpID, p_EmpName, p_EmailAddress);

SELECT 'Data inserted successfully' AS Message;


END

2. Stored Procedures

CREATE DEFINER=`root`@`localhost` PROCEDURE `Get_Country_Payments`(`Mention_year`


int, `Mention_country` varchar (20))
BEGIN
WITH CTE as
(select year(paymentDate) as `Year`, country , amount
from customers c join payments p on c.customerNumber = p.customerNumber)
select `Year` , country, concat(round(sum(amount)/1000),"K") as TotalAmount from
CTE
group by `Year`, country having `Year` = `Mention_Year` and country =
`Mention_Country`;
END

3.Trigger

CREATE DEFINER=`root`@`localhost` TRIGGER `emp_bit_BEFORE_INSERT` BEFORE INSERT ON


`emp_bit` FOR EACH ROW BEGIN
If New.Working_Hours <0 then
set New.Working_Hours = -(new.working_Hours);
End if;
END

You might also like