Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MS SQL: Suppress return value of stored procedure called in stored procedure

I think I have the same problem as kcrumley describes in the question "Problem calling stored procedure from another stored procedure via classic ASP". However his question does not really include an solution, so I'll give it another shot, adding my own observations:

I have two stored procedures:

CREATE PROCEDURE return_1 AS BEGIN
    SET NOCOUNT ON;
    SELECT 1
END

CREATE PROCEDURE call_return_1_and_return_2 AS BEGIN
    SET NOCOUNT ON;
    EXEC return_1
    SELECT 2
END

Note that both procedures contain "SET NOCOUNT ON". When I execute "call_return_1_and_return_2" I still get two record sets. First the value 1, then the value 2.

That throws ASP (classic VBScript ASP) off the tracks.

Any hints on how I can suppress the first result set? Why is it there even with NOCOUNT?

Skipping the first record set in ASP is not an option. I need a "database only" solution.

like image 622
BlaM Avatar asked Mar 03 '09 11:03

BlaM


People also ask

Is it mandatory to return value in stored procedure?

The RETURN statement is used to unconditionally and immediately terminate an SQL procedure by returning the flow of control to the caller of the stored procedure. It is mandatory that when the RETURN statement is executed that it return an integer value. If the return value is not provided, the default is 0.

Can stored procedure return value in SQL Server?

Return Value in SQL Server Stored ProcedureIn default, when we execute a stored procedure in SQL Server, it returns an integer value and this value indicates the execution status of the stored procedure. The 0 value indicates, the procedure is completed successfully and the non-zero values indicate an error.

Can we call stored procedure inside stored procedure?

Yes , Its easy to way we call the function inside the store procedure. for e.g. create user define Age function and use in select query.

Can we use return in stored procedure?

You can use one or more RETURN statements in a stored procedure. The RETURN statement can be used anywhere after the declaration blocks within the SQL-procedure-body. To return multiple output values, parameters can be used instead. Parameter values must be set before the RETURN statement runs.


1 Answers

As Matt points out in his comment, neither solution really 'swallow' the first resultset. I don't know why you'd want this but you can 'swallow' the result of the first exec by using a table variable. It must match the exact amount and type of the result set's columns. Like so:

CREATE PROCEDURE return_1 AS 
    SET NOCOUNT ON;
    SELECT 1
GO
CREATE PROCEDURE call_return_1_and_return_2 AS 
    SET NOCOUNT ON;
    DECLARE @Result TABLE (res int)
    insert into @Result EXEC return_1
    SELECT 2
GO
like image 61
edosoft Avatar answered Sep 18 '22 08:09

edosoft