How to get the stored procedure result set value - sql

I create a stored proc A that calls another proc B, which returns a list of the result set.
How I can use these result set values in my proc A as I have to use this result set value one by one to pass to other part of the proc A.

You can perform an INSERT INTO to insert the results of the stored procedure into a (temporary) table. You can then use a select statement to process these results.
INSERT INTO SomeTableThatMatchesTheSproc
EXEC YourStoredProcedure;
SELECT * FROM YourTable;

Related

Execute a stored procedure from within another stored procedure's SELECT statement?

I would like to execute a stored procedure X from within the SELECT statement of stored procedure Y, so that X's value can be returned as part of Y's data.
I am trying the following syntax, but it's apparently not valid.
SELECT name, type, (EXEC X #type=type)
FROM table
As I hope you can see above, I need to pass the current row's type value to procedure X to get the proper return value.
Disclaimer: I probably just don't know what I'm doing.
The approach what you have tried is invalid. Instead of the X as the stored procedure convert it as user-defined function. like the below
Create function dbo.fnGetTypeDetail
(
#type varchar(50)
)
returns varchar(100)
As
Begin
return --do your operation;
End
And replace your query as:
SELECT name, type, dbo.fnGetTypeDetail(type) AS TypeDetail
FROM table
For sample, I created a scalar function. Based on your requirement you can create inline table valued function as per the example
You can't EXEC a stored proc inside a SELECT statement.
What you can do is INSERT..EXEC a stored proc into a temp table, and then run a SELECT statement that queries that temp table, while joining to other tables if desired.
Psuedo-example:
INSERT INTO #Tmp (Column1) EXEC X;
SELECT Name, Type, (SELECT Column1 FROM #tmp)
FROM MyTable

Get count of records of a table which return from stored procedure

I have a stored procedure, which is returning a table. I just want the count of the records is it possible
My Procedure
create procedure Test
as begin
select * From Student
end
exec Test
will give the records out put
I want the count
NB: I need the sp to return the results of select statement.In another place I need the count of the records returned by sp and columns in the student table is dynamic.
I am expecting an answer without modifying stored procedure.
You can select the data into a temporary table like below. However, you have to use OPENQUERY to do so. You must also enable data access on your server first.
Exec sp_serveroption 'ServerName', 'data access', 'true'
SELECT * INTO #TempTable
FROM OPENQUERY("ServerName", 'EXEC Test')
SELECT COUNT(*) FROM #TempTable
NB: I need the sp to return the results of select statement.
Use your stored procedure as is. That is,
create procedure Test
as begin
select * From Student
end
In another place I need the count of the records returned by sp and
columns in the student table is dynamic.
If this other place is another SP, then use rowcount. You use it this way :
EXEC [sp_WhateverTheSPNameIs]
select ##rowcount
Read about ##RowCount. This is all you need.

How to stop results of nested stored procedure showing up at top level?

I've made an alteration to an existing stored procedure (dbo.pr1) so that it now calls a second stored procedure (dbo.pr2).
Both of these stored procedures return data with a final SELECT query.
In dbo.pr1 I've now added the line:
EXEC #var1 = dbo.pr2
I've done this in order to assign the values in dbo.pr2 to the variable #var1 (dbo.pr2 returns a single bit).
However, now when I execute dbo.pr1 I get two results back instead of the expected one. I get the SELECT query results at the end of dbo.pr1 but I also get the SELECT query result from dbo.pr2.
I cannot alter dbo.pr2 as it's being used elsewhere in the system. Is there a way that I can stop its result showing up when I execute dbo.pr1?
Do something with what is returned in the caller,
SET NOCOUNT ON;
...
DECLARE #resultsOfPr2 TABLE
(
...
);
INSERT #resultsOfPr2
EXEC #var1 = [dbo].[pr2];
Fiddle here
Note: I'm assuming [dbo].[pr2] selects a single result set.

Can I search stored procedure results?

Let's say I have a stored procedure which returns a large set of data. Can I write another query to filter the result of stored procedure?
For example:
select * from
EXEC xp_readerrorlog
where LogDate = '2011-02-15'
You would need to first insert the results of the stored procedure on a table, and then query those results.
create table #result (LogDate datetime, ProcessInfo varchar(20),Text text)
INSERT INTO #Result
EXEC xp_readerrorlog
SELECT *
FROM #Result
WHERE datepart(yy,LogDate) = '2012'
You can't make it part of a query, BUT you could insert the resulting data into a temp table or table variable and then use that for your query.
Does returning the error log for just an entire day make the result any more useful? I think it will still be full of useless entries. If you're looking for specific events, why not use one of the filter parameters for xp_readerrorlog? The following wil return all rows in the current log that contain the string 'fail':
EXEC xp_readerrorlog 0, 1, 'fail';
You can copy output from sp to temporaty table.
insert into #temp
EXEC xp_readerrorlog
and then use where clause with the temp table
or you can make a Table-valued Function

Insert into table from stored proc

I am trying to use a stored procedure that contains two different cursors as table input like such:
INSERT INTO table1 EXEC * FROM tblDailySales
The stored proc contains two cursors - I did not run just using.
I get the following error:
A cursor with the name 'csrDistricts' does not exist.
I also, get this error
An INSERT EXEC statement cannot be nested
The stored proc contains no EXEC that I can see.
What kind of stored proc other than simple SELECT can be used as source for table?
Is table1 already defined? If so all you should have to do is
INSERT INTO table1
EXEC storedProcedureName
Now, the trick is, the stored procedure will only be able to return one result set and insert into the table.
If you need to insert two different result sets, you'll have to gather then in two different stored procedures, then run two INSERT statements.
If you must do them at once, you'll need to do the insert from within the stored procedure.