Apply mysql stored procedure on a table column - mysql

I have a mysql stored procedure which accepts a variable and returns a result (the result is produced by a query):
mysp (var1) => col1,col2
I want to feed the variable from a table to produce an enhancement query such :
table1 => colA,colB,var1
select colA,colB, mysp (var1) from table1
is that possible ?

IMHO possible options are:
Move select to your stored procedure if possible and return final result set;
From your SP mysp create and populate temp table. Call procedure. Then join temp table in your select. Note: Temp tables have session scope.

Related

Call Stored Procedure While Inserting

I am trying to insert values from one table to another one using the following insert sql query
INSERT INTO [dbo].[table2]
SELECT Exec [StoredProcedure],
[Column1]
,[Column2]
FROM [dbo].[table1]
[table2] has the following columns:
RecNo, <-- INT
Column1, <--VARCHAR(50)
Column2 <--VARCHAR(50)
[StoredProcedure] main purpose is that every time a new row to be inserted in table2 it selects the maximum value from RecNo from table2 and adds 1 to that number to create the next number (sequential).
Here is the script for the stored procedure .
GO
ALTER PROCEDURE [dbo].[UpdateRcnoNumbers]
#MaxRcno INT OUTPUT
AS
BEGIN
SELECT #MaxRcno=MAX(Recno) FROM [table2]
SELECT #MaxRcno=#MaxRcno+1
RETURN #MaxRcno
END
But I am getting an error and I am not able to call the stored procedure ? Any suggestion please .
Thank you in advance
You should write a function for this purpose , read here
The problem is, Stored Procedures don't really return output directly. They can execute select statements inside the script, but have no return value.
MySQL calls stored procedures via CALL StoredProcedureName(); And you cannot direct that output to anything, as they don't return anything (unlike a function).
Here
MySQL Call Command

Nested SP to return table data

I want to return some ids from stored procedure, which would be used in "in Clause-Mysql".
Ex.
create procedure getid
as
begin
select empid from table
end;
create procedure getdata
As
begin
select * from employees where empid in (call getid(3))
end;
the above sample is simple scenario, but i want to implement a complicated query ,so i cannot join in getdata storedprocedure. simply i want to use nested sp which returns table of data should be used in in clause.
Although a mysql procedure can return a resultset (not a table), you cannot use it as a table in another query or stored procedure. Mysql functions cannot return table either.
You either have to use a view instead of a stored procedure or the stored procedures must use the same temporary table to pass data to each other.

Querying on the stored procedure result set

Im using SQL Server 2008. I have a result set from the stored procedure and i want to fire some more queries on the result set for e.g order by desc /asc and some querying. So what is the best way to do it. Using views or by using OPENQUERY.
Please help.
You can save results of stored procedure calls in any table or table variable that has same number and type of fields as the result set returned by the stored procedure, for example:
CREATE TABLE #temp(col1 INT, col2 VARCHAR(10))
INSERT INTO #temp(col1, col2)
EXEC usp_Proc1(#param1)
SELECT *
FROM #temp
Condition is that usp_Proc1 returns rows consisting of columns of type INT and VARCHAR(10) (in that order).

MySQL: count number of rows returned by a procedure

I want to Insert the records returned by procedure in to temporary table in another procedure
I have a Procedure Like Below
call getName()
Now I want to Check whether the above procedure is returning records or not.
I want to do this in another procedure. So I am trying to insert the records returned from getName() procedure in to temp table in another procedure to carry out count operation.
Let me know how to count no of rows returned by one procedure in another procedure.
Could you let me know how to insert values into temp table from stored procedure in Mysql
create temporary table <name> select * from <some results>
Let me know how to count no of rows returned by one procedure in another procedure.
select count(*) from <name>

SQL - Get stored procedure multiple record sets to temp table variable

Is it possible to get stored procedure multiple result set to temp table variable?
I wrote a stored procedure like,
Create proc test1
(
#param1 int,
#param2 int
)
as
Begin
Select Id, Name from Table1 where column1=#param1;
Select Id, Age, Address from Table2 where column1=#param2;
End
When i execute this sp it will return 2 tables(record set's).
Now, i want to get this 2 record sets to 2 temp table variables.
How can i achieve this?
I don't believe a stored procedure can return multiple result sets like you want it to. What I might suggest is to have the stored proc store the results in two global temp tables and then have the calling process (whatever is calling your stored proc) query the two global temp tables and put the results into your temp table variables.
I know this is an old question, and maybe I am misunderstanding what you want, but if you just want to take those two recordsets and put them into temp tables, couldn't you do this:
Create proc test1
(
#param1 int,
#param2 int
)
as
Begin
Select Id, Name
into #temp1
from Table1 where column1=#param1;
Select Id, Age, Address
into #temp2
from Table2 where column1=#param2;
End
now, I dont know what you're trying to achieve with this, but depending on how you're calling that sp, the #temp tables may not be accessible from outside of the sp.