How to call a stored procedure using select statement in mysql - mysql

I have call statement like
CALL report_procedure
('2013-02-01',now(),'2015-01-01','1');
and i want to use it in a select query.
i have tried like
Select * from ( CALL report_procedure
('2013-02-01',now(),'2015-01-01','1'));
but error occurs.
like
Error Code: 1064. You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near ( CALL report_procedure
('2013-02-01',now(),'2015-01-01','1') at line 3 0.297 sec
Can anyone suggest me a method to call stored procedure in Select statement in mysql??

It is not possible to use result set from procedure in FROM clause. MySQL does not allow doing this.
You may populate another table (or temporary table) in your procedure, and after, use that table in SELECT commands -
CALL report_procedure ('2013-02-01',now(),'2015-01-01','1'); -- fill temp_table
SELECT * FROM temp_table;

--Firstly your store procedure should look something like this:
CREATE PROCEDURE report_procedure(
IN d1 DATE,
dnow DATE,
d2 DATE,
val INT
)
BEGIN SELECT *
FROM yourtablename
WHERE date1 = d1
AND datenow > dnow
AND date2 > d2
AND value = val;
END
--The store procedure contains the select statement.
-- then you can call the store procedure like that:
CALL report_procedure('2013-02-01',now(),'2015-01-01','1');
--hope it helps

Related

MySQL | MariaDB Creates a #1064 syntax error on import [duplicate]

I am having some trouble with creating a Stored Procedure for a MySQL database.
Here is a Select statement that works:
use canningi_db_person_cdtest;
SELECT *
FROM pet
WHERE name = 'Puffball';
Here is my Stored Procedure that does not work:
use canningi_db_person_cdtest;
CREATE PROCEDURE GetAllPets()
BEGIN
SELECT *
FROM pet
WHERE name = 'Puffball';
END
I am getting the following error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near '' at line 5
How can I get this working?
EDIT
How do I call this Stored Procedure? I have tried this with no result:
use canningi_db_person_cdtest;
CALL GetAllPets();
This is the error:
#1312 - PROCEDURE canningi_db_person_cdtest.GetAllPets can't return a result set in the given context
Add a delimiter to end your procedure. Example:
use canningi_db_person_cdtest;
DELIMITER //
CREATE PROCEDURE GetAllPets()
BEGIN
SELECT *
FROM pet
WHERE name = 'Puffball';
END//
If you need more information, please refer to http://dev.mysql.com/doc/refman/5.1/en/stored-programs-defining.html
In this particular case, since you have only one statement in your procedure, you don't need to change DELIMITER nor use BEGIN...END block.
You procedure might look like this
CREATE PROCEDURE GetAllPets()
SELECT *
FROM pet
WHERE name = 'Puffball';
And use it like this
CALL GetAllPets();
Here is SQLFiddle demo

select from temporary table after call in mysql procedure

I am trying to make a reusable mysql procedure that takes data from join tables and store that in a temporary table. I will then create different procedures to select/calculate data from the result.
Here is my SQL code so far:
DELIMITER //
CREATE PROCEDURE getModulesForCourseYear(IN cid VARCHAR(10), IN yr INT(1))
CALL getModulesInCourse(cid);
SELECT * FROM modules WHERE year = yr;//
DELIMITER ;
The getModulesInCourse procedure creates a temporary table called modules
After getModulesInCourse is called in the procedure getModulesForCourseYear, I would then like to filter this result. This is where it fails.
I guess this is happening because mysql does not know what table modules is as it does not currently exist.
How would I be able to select from a temporary table in this procedure?
I am doing it in PHPMyAdmin which gives this error:
SQL query:
CREATE PROCEDURE getModulesForCourseYear(IN cid VARCHAR(10), IN yr INT(1))
CALL getModulesInCourse(cid);
SELECT * FROM modules WHERE year = yr;
MySQL said:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT * FROM modules WHERE year = yr' at line 3
NOTE: I am doing the procedure this way as I want to select the same initial data in multiple procedures, then I will filter it down. This way, it prevents duplicate code and if I need to change it later on, I can do so once.

MYSQL: stored procedure not executing query correctly with 'IN' clause correctly

I'm trying to pass a string value to a stored procedure from my code but it is not executing the query correctly. Following is the code of stored procedure.
I want to pass multiple values to the stored procedure like '2,3,4'. But when I do it only takes 2 and throws an error.
CREATE PROCEDURE `USP_INSERT_PROCESSED_ETC_RECORDS_TO_MAIN_TABLE`
(
IN RECORD_ID VARCHAR(20)
)
BEGIN
SELECT * FROM TX_TOLL_TRANSACTION_RECORD_DETAIL_STAGING
WHERE ID IN (RECORD_ID);
END
You have to use FIND_IN_SET() function instead of IN operator
Try this:
CREATE PROCEDURE `USP_INSERT_PROCESSED_ETC_RECORDS_TO_MAIN_TABLE`
(
IN RECORD_ID VARCHAR(20)
)
BEGIN
SELECT * FROM TX_TOLL_TRANSACTION_RECORD_DETAIL_STAGING
WHERE FIND_IN_SET(ID, RECORD_ID);
END

MySQL server stored procedure syntax error

I am having some trouble with creating a Stored Procedure for a MySQL database.
Here is a Select statement that works:
use canningi_db_person_cdtest;
SELECT *
FROM pet
WHERE name = 'Puffball';
Here is my Stored Procedure that does not work:
use canningi_db_person_cdtest;
CREATE PROCEDURE GetAllPets()
BEGIN
SELECT *
FROM pet
WHERE name = 'Puffball';
END
I am getting the following error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near '' at line 5
How can I get this working?
EDIT
How do I call this Stored Procedure? I have tried this with no result:
use canningi_db_person_cdtest;
CALL GetAllPets();
This is the error:
#1312 - PROCEDURE canningi_db_person_cdtest.GetAllPets can't return a result set in the given context
Add a delimiter to end your procedure. Example:
use canningi_db_person_cdtest;
DELIMITER //
CREATE PROCEDURE GetAllPets()
BEGIN
SELECT *
FROM pet
WHERE name = 'Puffball';
END//
If you need more information, please refer to http://dev.mysql.com/doc/refman/5.1/en/stored-programs-defining.html
In this particular case, since you have only one statement in your procedure, you don't need to change DELIMITER nor use BEGIN...END block.
You procedure might look like this
CREATE PROCEDURE GetAllPets()
SELECT *
FROM pet
WHERE name = 'Puffball';
And use it like this
CALL GetAllPets();
Here is SQLFiddle demo

How to use a procedure into the select and insert?

I'm work with store procedures, supose that I've the following procedure that return a value, and this value I use in other query.
CREATE PROCEUDRE filter(IN string varchar(1000), OUT salida varchar(1000))
BEGIN
.....
END
And I want make a insert with a select query for example:
INSERT INTO otherTable
SELECT filter(concat_group(column)) , value1,value2 from mytable
GROUP BY column,value,value2;
which is the correct way to do this?
Generally, you cannot call a stored procedure in the SQL select statement. What you want is like custom scalar functions.
reference
mysql scalar function with for loop