nested procedures on mysql - mysql

I am new to MySQL. I have started making a database with a table Customers which has two procedures.
getprocedure12 which returns name of customer when inputted the customer id
getcustage which returns age of customer when id is inputted.
so I want to make a 3rd procedure calling these two procedures which will output both name and age when id is given.
can you please help me figure out how to get the output I want?
CREATE PROCEDURE `nestedprocedurecustomers` (IN ID INT)
BEGIN
DECLARE (customername varchar(20), customerage INT);
CALL getcustomername12( cust_id, customername);
CALL getcustage( cust_id, customerage);
WHERE cust_id= ID;
END

You can't use WHERE outside a query. There's no need for the cust_id variable, just use id.
Finally, since this procedure doesn't have any OUT parameters, you need to use SELECT to return the variables that were returned by the inner calls.
CREATE PROCEDURE `nestedprocedurecustomers` (IN ID INT)
BEGIN
DECLARE customername varchar(20);
DECLARE customerage INT;
CALL getcustomername12(id, customername);
CALL getcustage(id, customerage);
SELECT customername, customerage;
END

Related

MySQL Using stored procedure in select statement

I have a stored procedure like so:
$connection->query('
drop procedure if exists listing_count;
create procedure listing_count(IN parent int(11))
begin
declare count1 int(11) default 0;
declare count2 int(11) default 1;
create temporary table ids as (select id from category where id=parent);
while count1<>count2 do
set count1=(select count(id) from ids);
insert into ids(id) select id from category where id not in(select id from ids) and related in(select id from ids);
set count2=(select count(id) from ids);
end while;
(select count(*) from listing_category where category in(select id from ids));
end');
$fetch=$connection->query('select *,listing_count(id) as listing_count from category')->fetchall(pdo::FETCH_UNIQUE|pdo::FETCH_ASSOC);
I would like to use my procedure like a function. So that listing_count gets the count so that I can use it. Do I need to create a separate function? Can a procedure get my count and return it?
Turning it into a function like so:
drop function if exists listing_count;
create function listing_count(parent int(11)) returns int(11) deterministic
begin
declare count1 int(11) default 0;
declare count2 int(11) default 1;
create temporary table ids as (select id from category where id=parent);
while count1<>count2 do
set count1=(select count(id) from ids);
insert into ids(id) select id from category where id not in(select id from ids) and related in(select id from ids);
set count2=(select count(id) from ids);
end while;
return (select count(*) from listing_category where category in(select id from ids));
end
But this does not work. I am not very familiar with procedures vs functions but I assume that I can't add all the functionality into a function as I can in a procedure.
I would like to use my procedure like a function.
You Can't Do Thatâ„¢.
I suggest you convert your sp to a stored function. That's a good idea in any case because it returns a single value. The way you have it now, it returns a one-column one-row result set. If it were a function it would work easily in every context you need it. In contrast, stored procedures returning result sets are not nearly as easy to use. For example, see this. How to use Table output from stored MYSQL Procedure
Or you could write a stored function to wrap your stored procedure and return the value. In my opinion that is an inferior solution, just because it has extra complexity.

Selecting all values from a table in mysql using a stored procedure

I have a table called Contacts with a field called person_id that I have connected to a java application.
If no value is specified for person_id in the application, I want to select everything from the contacts table using a stored procedure.
The operation I want to perform is this:
Select * from Contacts where (person_id like "%")
For this I have written a stored procedure shown below:
Delimiter $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `selectTest2`(In p_id int(11))
BEGIN
if p_id = null then
set p_id = "%";
end if;
select * from Contacts where (person_id like p_id);
END $$
Delimiter ;
However when I run this procedure in my sql using the following
call selectTest2(null)
The table that is returned is blank. How do I make it show all the values in the table?
The parameter p_id gets its value from a text box in the application. If the user has entered an id, I want the procedure to show only that particular record else I want it to show all records.
What have I done wrong and how do I correct it? I am aware that p_id is an int however I tried the same thing with other fields of type varchar and the table failed to return any value.
Try using case statement in where clause like below
WHERE CASE WHEN p_id IS NOT NULL THEN person_id = p_id ELSE TRUE END
Hope this should solve your problem

Use value of one procedure in another procedure

I am calling one procedure from another
CREATE PROCEDURE choice_select(IN choice_value BIGINT)
BEGIN
DECLARE AA varchar(100);
CALL TEST(choice_value,AA);
SELECT description
FROM academic
where {select a};
END
TEST is my another procedure and I want to pass the value returned from TEST procedure; in the WHERE clause, wherea is an output variable.
Plan A:
Use FIND_IN_SET function to filter records -
BEGIN
SET #aa = NULL;
CALL test(#aa); -- test should return a string like this - '1,2,3,4,5'
SELECT description FROM academic WHERE FIND_IN_SET(academic_id, #aa);
END
Plan B:
Populate a (temporary) table in the test procedure, then join this table with academic table.

How to retrieve multiple rows from stored procedure in MySQL?

Am trying to fetch out a field through stored procedure and I used following query. I aimed at fetching out multiple rows, but it executes the result successfully only when a single row exists. Or else it returns an error as I mentioned below.
MySQL Query
DELIMITER ;;
DROP PROCEDURE IF EXISTS Sample1;;
CREATE PROCEDURE Sample1(IN lft1 INT, IN rgt1 INT, OUT emp1 VARCHAR(20))
BEGIN
SELECT p.emp INTO emp1
FROM personnel p
WHERE p.lft > lft1
AND p.rgt < rgt1
LIMIT 10;
END;;
CALL Sample1(1, 10, #emp);;
SELECT #emp;
Error Message
MySQL said: Documentation
#1172 - Result consisted of more than one row
NOTE
Sample1 - procedure name
emp - selected field from table personnel
lft - use to check the condition, it is also one of the field of table personnel
personnel - table name
The error is not in your procedure. The error is in your query - it returns more then one row, but you cannot set multiple result into scalar value 'emp1'.
You should limit your query so that it returns one row.
How to retreive multiple rows from stored procedure in mysql?
Plan A: Fill another table, it may be a temporary table.
Plan B: Just execute your SELECT statement without INTO clause from the procedure; then you could read data-set from the application (c#, PHP+mysqli,...)
Plan C: Do not use the procedure, just execute the SELECT query.
Just had the same question. After a little research I found a solution in the official MySQL documentation:
Calling Stored Procedures with Statement Objects
It requires MySQL 5.5.3 or higher.
In contrast to the inital stored procedure from #Bala.C it doesn't use an out parameter.
CREATE PROCEDURE get_data ()
BEGIN
SELECT Code, Name, Population, Continent
FROM Country
WHERE Continent = 'Oceania'
AND Population < 10000;
SELECT Code, Name, Population, Continent
FROM Country
WHERE Continent = 'Europe'
AND Population < 10000;
SELECT Code, Name, Population, Continent
FROM Country
WHERE Continent = 'North America'
AND Population < 10000;
END;
You can use a cursor in MySQL.
CREATE PROCEDURE Sample1(IN lft1 INT,IN rgt1 INT, OUT emp1 VARCHAR(20))
BEGIN
DECLARE p_emp INT;
DECLARE cur_emp CURSOR FOR
SELECT p.emp
FROM personnell p
WHERE p.lft > lft1
and p.rgt < rgt1
LIMIT 10;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET noMoreRow = 0;
OPEN cur_emp;
LOOPROWS: LOOP
IF noMoreRow = 0 THEN
CLOSE cur_emp;
LEAVE LOOPROWS;
END IF;
FETCH cur_emp INTO p_emp;
SELECT p_emp;
END LOOP;
END;;

Linq function based on stored procedure which does count(*) returns null

I have a SQL Server 2008 stored procedure which does a select count(*) ...
I have a LINQ function called GetCount() based on that SP.
When I do something like:
int? count = datacontext.GetCount().Single().Column1;
count is null. The SP never returns null.
Why is count null?
It worked when I gave an alias to the count.
It appears that Linq-to-SQL doesn't like Stored Procs that return a single result as a SELECT statement. I had the same trouble you're seeing.
One solution for a case like this would be to create a stored function instead:
CREATE FUNCTION dbo.GetCountFunc()
RETURNS INT
AS BEGIN
DECLARE #MyCount INT
SELECT #MyCount = COUNT(*) FROM Customers
RETURN #MyCount
END
and then pull that into your Linq-to-SQL data context and reference it. In this case, I get back an INT no problem.
Marc
This link helped me http://blogs.msdn.com/b/wriju/archive/2009/04/10/linq-to-sql-returning-scalar-value-from-stored-procedure.aspx
basically if you are trying to return an int from a stored procedure it needs to look like this
CREATE PROCEDURE [dbo].[GetEmployeeCountRet]
AS
BEGIN
DECLARE #Ret INT
SELECT #Ret = COUNT(*) FROM Emp
RETURN #Ret
END