MySql stored procedures: How to select from procedure table? - mysql

Let's say we have a stored procedure selecting something from a table:
CREATE PROCEDURE database.getExamples()
SELECT * FROM examples;
How can I use the result from this procedure in a later select?
(I've tried
SELECT * FROM (CALL database.getExamples())
but with no success.)
Should I use SELECT... INTO outVariable in the procedure? Or should I use a function returning the table instead?

Reformulated the question in this thread: Can a stored procedure/function return a table?.
Obviously, it isn't possible without the use for temp tables.

CREATE TABLE #TempTable
(OID int IDENTITY (1,1),
VAr1 varchar(128) NOT NULL,
VAr2 varchar(128) NOT NULL)
Populate temporary table
INSERT INTO #TempTable(VAr1 , VAr2 )
SELECT * FROM examples

In SQL server you can then do SELECT * FROM database.getExamples()
If you want to re-use the 'procedure' then, yes, I would put it into a table valued function.
Otherwise you could just SELECT INTO a #temporary table inside the stored procedure.

Related

Can I use select sql output as an input to Insert sql, in a single stored procedure?

I wanted to retrieve a value from the select statement and use it in the insert statement. But, both these operations to be done in a single stored procedure. I'm trying something like below.
CREATE PROCEDURE Illness_sp (IN DoctorNameIn varchar(255), IN IllnessNameIn varchar(255), IN IllnessSevIn int(10))
BEGIN
select doctorId from doctor where DoctorName=DoctorNameIn;
INSERT INTO Illness(IllnessName, IllnessSeverity, PreferredDoctorId) Values(IllnessNameIn, IllnessSevIn, doctorId);
END
I have two statements inside this stored procedure:
retrieving doctorId from the SELECT statement; and
wanted to INSERT it into another table in the second statement.
INSERT INTO `Illness`(IllnessName, IllnessSeverity, PreferredDoctorId)
SELECT IllnessName, IllnessSeverity, PreferredDoctorId
FROM `SomeTable`
WHERE ...

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

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).

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.

User-defined Table Variables in MySQL 5.5?

I've recently moved from MSSQL to MySQL.
I would like to use a table variable (or equivalent) inside a MySQL 5.5 stored routine, to populate a dataset for an online report.
In MS SQL, I would do it this way
...
...
DECLARE #tblName TABLE
WHILE <condition>
BEGIN
Insert Row based on iteration value
END
...
...
From what I understand, I can't declare table variables in MySQL (correct me if I'm wrong) How do I implement the above logic in a MySQL stored procedure?
You could create a table or temporary table and populate it with data you need.
CREATE TABLE Syntax
You understand that limitation correctly. The MySQL user manual clearly states that user-defined variables cannot refer to a table:
http://dev.mysql.com/doc/refman/5.5/en/user-variables.html
User variables are intended to provide data values. They cannot be used directly in an SQL statement as an identifier or as part of an identifier, such as in contexts where a table or database name is expected, or as a reserved word such as SELECT.
create temporary table tmp
(
id int unsigned not null,
name varchar(32) not null
)
engine=memory; -- change engine type if required e.g myisam/innodb
insert into tmp (id, name) select id, name from foo... ;
-- do more work...
select * from tmp order by id;
drop temporary table if exists tmp;
I think this covers it. Also, this may be helpful.