Calling a user defined stored procedure from select statement mysql - mysql

I am trying to call a user defined stored procedure from a select statement and its giving me an error. However when I call a system procedure it works just fine. Is there a way to call a user defined procedure from a select statement. This is for mysql
SELECT ID, email FROM user PROCEDURE simpleproc();
give me an error ERROR 1106 (42000): Unknown procedure 'simpleproc'
mysql> call simpleproc();
Query OK, 0 rows affected (0.21 sec)
where as
SELECT ID, email FROM user PROCEDURE ANALYSE();
works

You can call stored procedure from select statement,To call a procedure you must use following syntax:
CALL stored_procedure_name (param1, param2, ....)
Such as, you can CALL following procedure:
DELIMITER //
CREATE PROCEDURE `procedure1`(IN var1 INT)
BEGIN
SELECT var1 + 2 AS result;
END//
as
CALL procedure1(10);
Check this site for reference: http://net.tutsplus.com/tutorials/an-introduction-to-stored-procedures/

Related

MySQL query does not execute if i use the Stored Procedure parameter in the query

I am creating a very simple store procedure with a query, but when i use the store procedure IN parameter in the query it gets stuck and does not execute the query, but if i put the value direct to the query it works.
This works:
CREATE PROCEDURE `cap-reports`.ffap_test()
BEGIN
select * FROM students WHERE name='Fernando';
END
This does not, i spent 10 minutes and it never returned
CREATE PROCEDURE `cap-reports`.ffap_test(IN pName VARCHAR(10))
BEGIN
select * FROM students WHERE name=pName;
END
call `cap-reports`.ffap_test('Fernando');
What mistake i am doing here? I never had this problem before
This procedure works for me. Maybe it's the difference in database of the procedure and the students table? Or a missing semi-colon?
CREATE PROCEDURE `cap-reports`.ffap_test(IN pName VARCHAR(10))
BEGIN
select * FROM `cap-reports`.members m WHERE m.Username = pName;
END
;
CALL `cap-reports`.ffap_test('winkbrace');

Stored Procedure MySQL Syntax error in Adminer

I'm very new to stored procedures in MySQL in general. I am trying to create one in Adminer and I keep getting a syntax error message:
Syntax error near '$$ CREATE PROCEDURE test() BEGIN SELECT * from lead; END$$ DELIMITER' at line 2
I'm trying to create a procedure named test to select all the records from a table. The code I put into Adminer is as follows:
DELIMITER $$
CREATE PROCEDURE test()
BEGIN
SELECT * from lead;
END$$
DELIMITER ;
However, if use the MySQL CLI, and enter the exact same code line for line, it works
mysql> DELIMITER $$
mysql> CREATE PROCEDURE test()
-> BEGIN
-> SELECT * from lead;
-> END$$
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
If I call the procedure test, I get all the records from the lead table.
I've logged into both Adminer and the CLI with the same credentials so I'm fairly confident it's not a permissions thing. I know I am missing something; can any one assist with a point in the right direction?

How to handle one Stored Procedure output (ResultSet or Table) into another Stored Procedure as Table

one Stored Procedure is returning me a table and I want to hold that result into another Stored Procedure.
and I am facing SQL syntax error.
First Stored Procedure
DELIMITER $$
USE `dataBase`$$
DROP PROCEDURE IF EXISTS `testReturnTable`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `testReturnTable`()
BEGIN
SELECT `user_id`, `email` FROM `users`;
END$$
DELIMITER ;
and I am trying to call this Store Procedure into another and want to hold the data into a view or table
DELIMITER $$
USE `dataBase`$$
DROP PROCEDURE IF EXISTS `testReturnCall`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `testReturnCall`()
BEGIN
DROP VIEW IF EXISTS `my_view`;
CREATE OR REPLACE VIEW my_view AS (CALL testReturnTable());
SELECT * FROM my_view;
END$$
DELIMITER ;
and getting error
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 testReturnTable());
I can use function here as well, but I don't know how to handle result set,
please advice.
it is not possible to get table ouput from one stored procedure to another stored procedure. But we have alternate to do this which is Temp table

Not allowed to return a result set from a trigger

I have problem:
I created procedure :
CREATE PROCEDURE `tran_sp` ()
BEGIN
SELECT 'procedure runned!';
END
$
It's works !
mysql> CALL tran_sp()$
+-------------------+
| procedure runned! |
+-------------------+
| procedure runned! |
+-------------------+
1 row in set (0.00 sec)
Then i created trigger :
mysql> CREATE TRIGGER my_trigger1
-> AFTER INSERT ON users
-> FOR EACH ROW
-> BEGIN
-> CALL tran_sp();
-> END$
mysql> INSERT INTO users VALUES(166,156)$
ERROR 1415 (0A000): Not allowed to return a result set from a trigger
mysql>
Help me please.
The exception is I think clear enough.
You can perform additional operations inside a trigger (call a SP, perform insert / update / delete operations, ...) but all of those aren't allowed to return any result.
This means, a SP with a simple select-statement inside isn't allowed. If instead you would use this select statement within a loop for instance in order to perform updates or similar, this would be allowed, as you wouldn't return anything.
The reason is, that an insert/update/delete statement can't return anything, it can't return the result set of your stored procedure and therefor you shouldn't try to return one inside the trigger.

stored procedure that takes Query results as parameter

i just created a stored procedure that take a parameter(example id) and copies columns related to that id from one table to another table.
How can i create stored procedure that takes sub query results as parameter,database is mysql..
This is my example..i want to pass query that select id from table to procedure..
DELIMITER $$
DROP PROCEDURE IF EXISTS `test`.`sasi`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `sasi`(IN idno int(4))
BEGIN
INSERT INTO user5(id,email,address,fullname,gender,phonenumber)
SELECT id,email,address,fullname,gender,phonenumber FROM user1 where id != idno;
END$$
DELIMITER ;
call sasi(4);
To pass the results of a query into your stored procedure, wrap the query in brackets.
For example:
call sasi((select max(id) from sometable where somecondition));
You must make sure the query only returns one row and one column.
Edited:
If you want to call the procedure multiple times, once for each row, change your procedure to be a FUNCTION:
CREATE FUNCTION sasi(idno int(4))
RETURNS int(4)
BEGIN
INSERT INTO user5(id,email,address,fullname,gender,phonenumber)
SELECT id,email,address,fullname,gender,phonenumber FROM user1 where id != idno;
RETURN idno;
END
Then call it like this:
select sasi(id)
from table
where ...
sasi(id) will get called for every row matching the where clause.