How to get row count of a stored procedure? - sql

I have a stored procedure which accepts a Client number and returns certain data. I have another stored procedure which internally calls the first one and needs to use the Row count from the first stored procedure.
Is there anywhere I can achieve this without using a temporary table (to get the result set) or using an OUT parameter in the first stored procedure.Thanks.

Since you already calling your stored procedure from first one, you already have ability to get number of rows returned by using ##ROWCOUNT function. Below is example of using `##RowCount
CREATE PROC test1
AS
BEGIN
SELECT *
FROM sys.tables
END
CREATE PROC Test2
AS
BEGIN
SET NOCOUNT ON;
EXEC Test1
PRINT ##ROWCOUNT
END
EXEC Test2
In my example I just printed that value out, but you can just as simply assign that to a variable in second procedure and use it for something else.

Related

Update from stored procedure return

I have a stored procedure that I want to run on every row in a table that matches a where clause, the procedure already exists on the server and is used in other places so it cannot be modified for these changes.
The stored procedure returns a scalar value, I need to store this value in a column in the table, I've tried using the update:
UPDATE tbl SET tbl.Quantity =
EXEC checkQuantity #ProductID = tbl.ProductID, #Quantity = tbl.Quantity
FROM orders tbl WHERE orderNumber = #orderNumber
But this of course doesn't work, is there a way to do this without multiple queries, reading the line info, running the proc in a loop then updating the original line?
No there is no way to do this without multiple queries. This is one of the few scenarios where a cursor or loop is necessary.
Unless you can replace your stored procedure with a user-defined function, which can be run in the context of a single query.

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.

How to get the stored procedure result set value

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;

How can a stored procedure return ROWCOUNT?

I have written stored procedure which has 2 Insert queries and 1 Update query inside it. Of all these,either insert queries or update query are executed at a time. Now my problem is to get ROWCOUNT in each case. Say suppose if insert operations are executed,then I want stored procedure to return ##ROWCOUNT to the calling application, so that the application will be aware of whether the required operations executed correctly or not. Can anyone suggest/tell me how can I get the rows affected from the stored procedure?
Use Output parameters in your stored procedures to return the RowCount of your inserts / updates.
Refer MSDN link for more information on how to use Output params
You can have multiple output params so you can have 2 different output params one each for your insert and the 3rd for your update statement.
Example:
CREATE PROCEDURE GetEmployeeData
#employeeID INT,
#managerID INT **OUTPUT**
AS
BEGIN
....
....
Additionally, you can always concatenate the rowcounts of your 2 Inserts / Update using delimiters and return them as one value eg: "10;0" - However that is the old fashioned and "I would not recommend" approach.
Also, you could create a table variable and return the table with rows = number of Inserts / updates and the value of the column = RowCount affected.

Stored Procedure returning Multiple resultset

One Stored procedure returning multiple result sets and I need only the last result set, How do I achieve this without changing original procedure. am using the last reulst set in further processing in other Stored procedure.
if you are "filling" a dataset in c#, very simple, just use:
datasetobj.Tables[datasetobj.Tables.Count-1].Table
to get the DataTable
if doing this within sql procedures (i.e. one procedure calling another that returns multiple), best solution would be to use output variables. concept:
procedure1 returns multiple resultsets
when calling:
declare #table1 table (), #table2 table ()
exec procedure1 out #table1, out #table2