User-Defined Functions Returning Table in MySQL - mysql

PostgreSQL allows you to define a function that returns a table. Does MySQL provide a similar feature? My research suggests not, but I'd be grateful if someone could show me otherwise.
Essentially, I want to add a "running-total" column to a rowset, and this is one of the options I'm investigating.

You can not return a table using MySQL function, but you can using a stored procedure, I got something like this:
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
And for more information please refer to this link

Related

MySQL Stored Procedure - if then not functioning

I'm converting SQL Server stored procedures to MySQL and running into issues. I have a stored procedure with an IF THEN ELSE that, while not giving errors, is not returning any data either and I'm not seeing the problem to fix it. The queries by themselves are correct and return data but don't seem to work in the stored procedure. This is a simplified version of the real query just FYI.
The SQL for creating the stored procedure is:
DROP PROCEDURE IF EXISTS `sp_GetVolunteerAwardsList`;
DELIMITER //
CREATE PROCEDURE `sp_GetVolunteerList`( IN glAward_in int)
BEGIN
DECLARE glAward_In INT;
DECLARE awardType_In varchar(100);
DECLARE awardActive INT;
IF (glAward_In) = 0 THEN
SELECT * FROM tbl_volunteer
ELSEIF (glAward_In) = 1 THEN
SELECT * FROM tbl_volunteerpositions
END IF;
END
//
As always, any assistance would be most appreciated.
Check the glAward_In parameter or variable.
The SP is receiving the parameter glAward_in, i in lower case.
Then there is a DECLARE that declares a different variable glAward_In, i in upper case.
The if is done using the glAward_In in uppercase which is not set in any place of the SP. And the parameter in lower case is not used anywhere in the SP.
I think you have to remove the DECLARATION of the variable in upper case, and use the parameter in lowercase for the IF evaluation.

How to get stored procedure result set to other stored procedure. Or return table in function in MySQL

My first stored procedure returns 3 values which I will use in another stored procedure. I tried like this:
CREATE DEFINER=`db`#`%` PROCEDURE `SP_Expense`(
flightInstanceId INT
)
begin
SELECT F.BaseFare, CALL SP_Orderdetail(F.fareId)
FROM fare;
end$$
but I'm getting a syntax error.
Is it possible in functions? Which is return table ?
You cant call a stored procedure from with in a select statement. What you are doing is trying to call a function. Create Fucntion. If I understand your question SP_Orderdetail(F.fareId) returns a table? I wonder if you could turn SP_Orderdetail(F.fareId) into a view instead?
This might help as well: Create Stored procedure and Create function syntax

Entity Framework stored procedure: Function Import to Complex Type, Error on Get Column Information

I am working on a project in VS2012 while using MySQL database and Entity Framework 5.0.0.
When I try to create a new Complex type from a stored procedure, I get an error when clicking "Get Column Information" button:
The selected stored procedure or function returns no columns.
My stored procedure code is as following:
DROP PROCEDURE IF EXISTS SearchAlgemeenSet;
Delimiter //
CREATE PROCEDURE SearchAlgemeenSet(IN in_searchQuery VARCHAR(255))
BEGIN
SELECT Blokken, Jaargang, Werk_Uren
FROM algemeensets
WHERE Blokken LIKE in_searchQuery
OR Jaargang LIKE in_searchQuery
OR Werk_Uren LIKE in_searchQuery;
END
//
Delimiter ;
I'm positive that it returns columns if the in_searchQuery parameter has a match.
In my research, I have found plenty solutions for Microsoft SQL database. But none of those solutions apply to MySQL database.
How to solve this?
I'm positive that it returns columns, if the in_searchQuery
parameter has a match.
Because you are not using any wild card for partial search, unless it finds an exact match, for in_searchQuery value, no rows will be returned. For partial match to happen, you need to use wild card symbol '%' with the in_searchQuery value.
Modified procedure, should be like the following:
DROP PROCEDURE IF EXISTS SearchAlgemeenSet;
Delimiter //
CREATE PROCEDURE SearchAlgemeenSet(IN in_searchQuery VARCHAR(255))
BEGIN
set #search_criteria := concat( '%', in_searchQuery, '%' );
SELECT Blokken, Jaargang, Werk_Uren
FROM algemeensets
WHERE Blokken LIKE #search_criteria
OR Jaargang LIKE #search_criteria
OR Werk_Uren LIKE #search_criteria;
END;
//
Delimiter ;
When no search criteria has any matches found, and empty set would be returned.
In your scripting language, you have to check, in advance, whether the procedure has returned any results or not. Based on that, you can show a message that no data found or you can perform other actions.

i bulid stored procedure in MySql server but doesn't work !

how to bulid a stored procedure in MySql server .
i wrote the stored procedure outside in note but i don't know how to apply this stored procedure in the server that i am doing the testin .
to be clear i download wampSever which includes Mysql ,then i created my tables. but again i don't know the steps that i have to take in orded to use the stored procedure.
my stored procedure:
CREATE PROCEDURE findHorse
#horseName varchar(15) AS SELECT * FROM horse
WHERE horseName =#horseName
i know that if i wante to execute it i use
exec findHorse
all what i need are :
1. how to create it in mySql. //steps
2.where should i put exec findHorse in order to execute it //steps
In MySQL you execute a stored procedure by 'calling' it:
CALL DoStuff(#Param1);
You create one as follows:
CREATE PROCEDURE DoStuff()
BEGIN
SELECT Column1
FROM MyTable;
END;
This might help
I believe this is what you are looking for:
DELIMITER $$
CREATE PROCEDURE `findHorse` (IN `inFindHorse` CHAR(255))
BEGIN
SELECT * FROM `horse` WHERE `horse`.`horseName` = `inFindHorse`;
END$$
DELIMITER ;
And as Randy noted, the correct way to call this procedure is:
CALL findHorse('Flicka');

Suppose I want to create a "stored function" for MYSQL. Where do I wrote it? Which file?

Of course, I could go into mysql console and type the Function. But what if I want to store it for future use? What do I do?
Most projects have an SQL file to initialize the database from scratch. This way, one can set up the application database by simply running this SQL file. Your CREATE PROCEDURE/FUNCTION query would also go in this file.
There's a good tutorial here:
http://www.databasejournal.com/features/mysql/article.php/3525581/MySQL-Stored-Procedures-Part-1.htm
You need to use stored procedures. Once written, these are stored in the database along with your tables. They can be invoked using the CALL <procedure> statement.
Here's an example procedure that populates table1 with random values:
DELIMITER //
DROP PROCEDURE IF EXISTS autofill//
CREATE PROCEDURE autofill()
BEGIN
DECLARE i INT DEFAULT 0;
WHILE i < 20000 DO
INSERT INTO table1 (size) VALUES (FLOOR((RAND() * 1000)));
SET i = i + 1;
END WHILE;
END;
//
DELIMITER ;
Once the procedure has been written, it is called like this:
CALL autofill();