Run procedure in MySQL - mysql

I created 3 procedures and the 3rd calls the other two:
pi function
DELIMITER $$
DROP PROCEDURE IF EXISTS pi$$
CREATE PROCEDURE pi()
BEGIN
DECLARE pi FLOAT;
SET pi = 3.14;
SELECT pi;
END$$
power function
DELIMITER $$
DROP PROCEDURE IF EXISTS pot$$
CREATE PROCEDURE pot(input_number INT)
BEGIN
DECLARE pot FLOAT;
SET pot= input_number;
SELECT POWER (pot,2);
END$$
areac function
DELIMITER $$
DROP PROCEDURE IF EXISTS areac$$
CREATE PROCEDURE areac(input_number INT)
BEGIN
DECLARE area FLOAT;
SET area = input_number;
SELECT pi()*pot(area,2);
END$$
When I run CALL areac(1)$$, this error appears:
ERROR 1305 (42000): FUNCTION aula3.pot does not exist
How can I fix this?

The first two statements create STORED PROCEDURE and not FUNCTION. Here's the syntax to create function.
For PI()
DELIMITER $$
CREATE FUNCTION pi()
RETURNS FLOAT
BEGIN
RETURN 3.14;
END $$
DELIMITER ;
For POT()
DELIMITER $$
CREATE FUNCTION pot(input_number INT)
RETURNS FLOAT
BEGIN
RETURN POWER(input_number, 2);
END $$
DELIMITER ;
and you can now execute this statement: CALL areac(1)

Related

How can I create a stored procedure in MySql?

I'm trying to create a stored procedure but i have an error: "Statement is uncomplete, expecting ";"
use kms;
create procedure getAllClients()
BEGIN
select * from Clients;
END;
Try this
use kms;
DELIMITER //
create procedure getAllClients()
BEGIN
select * from Clients;
END//
DELIMITER ;

how to pass parameter to stored procedure using mysql

Im trying to add parameter to this stored procedure
DELIMITER $$
DROP PROCEDURE IF EXISTS `mifostenant-default`.`test` $$
CREATE PROCEDURE `mifostenant-default`.`test` ()
BEGIN
select * from employees
END $$
DELIMITER ;
Here is the way to make a procedure and pass a parameter to it:
delimiter $$
DROP PROCEDURE IF EXISTS `mifostenant-default`.`test`;
CREATE PROCEDURE `mifostenant-default`.`test` (IN empName CHAR(20))
BEGIN
SELECT * FROM employees WHERE employee_name=empName;
END $$
delimiter ;
And you call your procedure as below:
CALL mifostenant-default.test(the_parameter_to_pass);

Stored Procedure using Select Error 1064

I have a simple procedure to select data from an column and show them
DELIMITER //
DROP PROCEDURE IF EXISTS `testing`
CREATE PROCEDURE `testing`(IN param1 VARCHAR(40))
BEGIN
SELECT * FROM testingdatabase,
END //
DELIMITER;
However I keep getting error 1064
SELECT * FROM testingdatabase;
use ; not ,
So it should be:
DELIMITER //
DROP PROCEDURE IF EXISTS `testing`//
CREATE PROCEDURE `testing`(IN param1 VARCHAR(40))
BEGIN
SELECT * FROM testingdatabase;
END //
DELIMITER ;
DELIMITER //
DROP PROCEDURE IF EXISTS `testing`//
CREATE PROCEDURE `testing`(IN param1 VARCHAR(40))
BEGIN
SELECT * FROM testingdatabase;
END //
DELIMITER ;
DELIMITER //
DROP PROCEDURE IF EXISTS testing//
CREATE PROCEDURE testing(IN param1 VARCHAR(40)) BEGIN
SELECT * FROM testingdatabase;
END //
DELIMITER ;
Check this
DELIMITER $$
CREATE
PROCEDURE `testing`(IN param1 VARCHAR(40))
BEGIN
SELECT * FROM `test_table` WHERE `test_cdeo` LIKE param1;
END$$
DELIMITER ;
You can call and check your procedure via sql command like below code
CALL testing('149514')

How to return the table using mysql Function

I am using MySQL. I want to return the table using MySQL function. In SQL its working fine but not in MySQL. I attach my partial code
DELIMITER $$
CREATE FUNCTION myFunction() RETURNS #tmptable TABLE (item varchar(20))
BEGIN
insert into #tmptable(item) values('raja')
return
END; $$
Using functions you can not return a table.
However you can use stored procedure to return the table.
DELIMITER $$
CREATE DEFINER=`root`#`%` PROCEDURE `sp_Name`(OUT po_ErrMessage VARCHAR(200))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
SET po_ErrMessage = 'Error in procedure sp_Name';
END;
SELECT * FROM table_name;
END

ERROR when send table as parameter in MySQL store procedure

I wrote a store procedure. It executes properly, but when I call the procedure, it shows the error:
Error Code : 1327 Undeclared variable:
Third
Please see my procedure below:
DELIMITER $$
USE `db_test`$$
DROP PROCEDURE IF EXISTS `test_proc`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `test_proc`(IN newsInfoTable VARCHAR(100))
BEGIN
SET #sql_stam = CONCAT('SELECT news INTO ', #news,' FROM ',newsInfoTable,' WHERE ',CURDATE(),'=?;');
PREPARE s1 FROM #sql_stam;
SET #where_param = DATE_FORMAT(date_time,'%Y-%m-%d');
EXECUTE s1 USING #where_param;
SELECT #news;
END$$
DELIMITER ;
Calling parameter:
USE db_test;
CALL test_proc('tbl_morning_news');
Change your code into:
DELIMITER $$
USE `db_test`$$
DROP PROCEDURE IF EXISTS `test_proc`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `test_proc`(IN newsInfoTable VARCHAR(100))
BEGIN
SET #sql_stam = CONCAT( 'SELECT news INTO #news FROM ',newsInfoTable
,' WHERE DATE(`date_time`) = CURDATE()' );
PREPARE s1 FROM #sql_stam;
SELECT #news;
END$$
DELIMITER ;