Querying on the stored procedure result set - sql-server-2008

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

Related

mysql - apply result of stored procedure (using dynamic sql) in an update query

I want to run a stored procedure for every row in a table and update the row with the result.
I picture something like this:
UPDATE `vouchers` SET `code` = (CALL voucherCode('db.vouchers', 3, 4))
WHERE `code` IS NULL;
Using a Function is not an option, as the stored procedure contains dynamic sql. It's also okay to use an output parameter if that is easier than using the result set. Currently the procedure is defined as below, but can easily be adopted to yield a single row result to behave as intended in the above code sample.
`voucherCode`( IN `tableName` VARCHAR(40),
IN `segments` INT(32), IN `segmentLength` INT(32), OUT `code` VARCHAR(32) )
The only solution I came up so far is to create another stored procedure which utilizes a cursor to iterate through the vouchers table, and call the voucherCode-procedure within the loop.
So what is the best way to call a stored procedure from within an insert or an update query?

MySQL Stored Procedures: Using a result set returned from a stored procedure in another stored procedure with MYSQL 5.6

In our MySql database there are 3 stored procedures Sp1(), Sp2() and Sp3() . There is logic in Sp3() which I want to use in the stored procedures Sp1() and Sp2(), I know that using a temporary table in Sp3() and using that temporary table in Sp1() and Sp2() works.
To save time and memory it is preferred not to create a temporary table.
One extra piece of info is I can return a result set from a stored procedure to my client. But I am unable to make this store procedure (SP3) work like a subquery, where the result set is returned from the Stored procedure Sp3() and can be compared with another table in Sp2() and Sp1() based on a id key.
I want to try something like below, which can be used in stored procedures Sp1() and Sp2():
Assuming Sp3() returns an id field along with other fields:
Select
id
from
EmployeeTable e
where 5000 >
(Select salary from (call Sp3()) where id= e.id);

OPENROWSET with no column alias?

I'm trying to write a query to select the results of a stored procedure into a temp table. However the stored procedure has been set up so that when it runs its returns 1 result with no alias. (See below)
obviously I get an error when I try and select the data into a temp table.
An object or column name is missing or empty. For SELECT INTO
statements, verify each column has a name. For other statements, look
for empty alias names. Aliases defined as "" or [] are not allowed.
Change the alias to a valid name.
Is there any way round this as I will be unable to update the procedure to output an alias! Basically Im after a way of doing a
SELECT * INTO #tmptable
FROM OPENROWSET ('SQLNCLI', 'Server=ServerName;Trusted_Connection=yes;','SET FMTONLY OFF EXEC sp_name')
If you know how many columns you will get back from the OPENROWSET, then you can create the temporary table before inserting values; this allows you to give the columns a name.
CREATE TABLE #tmptable (Value INT NOT NULL)
INSERT #tmptable
SELECT * FROM OPENROWSET ('SQLNCLI','Server=ServerName;Trusted_Connection=yes;','SET FMTONLY OFF EXEC sp_name')
-- DROP TABLE #tmptable
If you do not know how many columns you are returning... I do not know that it is possible.

Apply mysql stored procedure on a table column

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.

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.