views:

50

answers:

2

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

alt text

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?

A: 

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

InSane
HMM... I already tried it. But there are some restrictions and it cause errors doing that kind of approach (http://www.sommarskog.se/share_data.html#INSERTEXEC). "It can't nest, Thus, you can only have one INSERT-EXEC active at a time."
yonan2236
@yonan2236 - do these stored procedures call other SP's as well, is it? Also, i assume you are not having any control over modifying these SP's, is it?
InSane
yes sir, it calls other SPs
yonan2236
+2  A: 

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);
}
Jesse C. Slicer
Exactly the answer I was about to post had I not found it.
James Dunne
ok sir, I'll try yours.. thanks.
yonan2236