Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return the table using mysql Function

Tags:

sql

mysql

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; $$
like image 359
Bharathi Avatar asked Nov 06 '12 13:11

Bharathi


People also ask

How do I execute a function in MySQL?

It's very simple to run the MySQL function. Instead of procedure we can add any multiple line function in above example. @Sebastianb, under the "delimiter //" prompt we can call functions as well. using CALL is just an example for reference of procedures.

How can I return multiple values from a function in MySQL?

MySQL stored function returns only one value. To develop stored programs that return multiple values, you need to use stored procedures with INOUT or OUT parameters. If you are not familiar with INOUT or OUT parameters, check it out the stored procedure's parameters tutorial for the detailed information.

How do you create a function in MySQL?

The syntax to create a function in MySQL is: CREATE FUNCTION function_name [ (parameter datatype [, parameter datatype]) ] RETURNS return_datatype BEGIN declaration_section executable_section END; function_name.


1 Answers

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
like image 59
Rahul Tripathi Avatar answered Nov 08 '22 21:11

Rahul Tripathi