简体   繁体   中英

How to get only one set of data from my Stored Procedure?

I'm using SQL Server 2000.
My SP produces the follwing output:

替代文字

It sums up all Taxable Earnings (top result) and subtract it by the sum of Deductible Expenses (middle result) to get the Net Taxable Income of an employee.

How will I get the value of Net Taxable Income only?

Here's my SP

/*
DECLARE @NET_TAXABLE_INCOME AS NUMERIC(19, 2)
EXEC NET_TAXABLE_INCOME '000001', '10/15/2010', @NET_TAXABLE_INCOME OUTPUT
SELECT @NET_TAXABLE_INCOME
*/
CREATE   PROC [dbo].NET_TAXABLE_INCOME
(
    @EMPLOYEENO AS VARCHAR(10),
    @PAYROLLPERIOD_INPUT AS DATETIME,
    @NET_TAXABLE_INCOME AS NUMERIC(19, 2) = NULL OUTPUT
)   
AS
BEGIN
    DECLARE @TAXABALEEARNINGS AS NUMERIC(18, 2)
    EXEC TAXABLE_EARNINGS_BREAKDOWN @EMPLOYEENO, @PAYROLLPERIOD_INPUT, @TAXABALEEARNINGS OUTPUT
    DECLARE @DEDUCTIBLEEXPENSES AS NUMERIC(18, 2)
    EXEC DEDUCTIBLE_EXPENSES_BREAKDOWN @EMPLOYEENO, @PAYROLLPERIOD_INPUT, @DEDUCTIBLEEXPENSES OUTPUT

    SET @NET_TAXABLE_INCOME = @TAXABALEEARNINGS - @DEDUCTIBLEEXPENSES   
    SELECT @NET_TAXABLE_INCOME AS [NET_TAXABLE_INCOME]
END

Is there a SQL statement that will cause to not to print the result of EXEC ?

On the C# side, you can do:

SqlDataReader sqlReader = sqlCmd.ExecuteReader();
sqlReader.NextResult(); // Skip first result set.
sqlReader.NextResult(); // Skip second result set.
while (sqlReader.Read())
{
    var netTaxableIncome = sqlReader.GetValue(0);
}

You can get the results of the executed Stored procedures into a Temporary table instead.

CREATE TABLE #Tmp (etc etc)

INSERT #Tmp
EXEC DEDUCTIBLE_EXPENSES_BREAKDOWN @EMPLOYEENO, @PAYROLLPERIOD_INPUT, @DEDUCTIBLEEXPENSES OUTPUT

But you can also combine it with the EXEC command as well, and that's the answer to capturing the output of the stored procedure. All you need to do is create a table to receive the output of the stored procedure via the INSERT statement:

The key is to create the table (a temporary table in this example) so that it has the right number of columns and compatible data types to capture the output of the stored procedure.

NOTE: syntax is psuedo syntax but you get the idea, i hope!

That way, you can then just discard these results - as that is what your intention is without affecting the output of your calling stored procedure!!

However, if these stored procedures are under your control, you should consider if you really want to reuse them / change then to only return the output param and not the resultset. You are un-necesssarily returning data that you are not going to use, atleast in this case

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM