如何从我的存储过程中仅获取一组数据?

发布于 2024-09-26 21:26:25 字数 1081 浏览 5 评论 0原文

我正在使用 SQL Server 2000。
我的 SP 产生以下输出:

alt text

它总结了所有应税收入(顶部结果)并减去可扣除费用之和(中间结果)即可得到员工的应税净收入

我如何仅获得应税净收入的值?

这是我的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

是否有一个SQL语句会导致不打印EXEC的结果?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏末 2024-10-03 21:26:25

在 C# 方面,您可以执行以下操作:

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

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);
}
﹂绝世的画 2024-10-03 21:26:25

您可以将执行的存储过程的结果放入临时表中。

CREATE TABLE #Tmp (etc etc)

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

但您也可以将它与 EXEC 命令结合使用,这就是捕获存储过程的输出的答案。您需要做的就是创建一个表来通过 INSERT 语句接收存储过程的输出:

关键是创建表(本例中为临时表),使其具有正确的列数和兼容的数据类型捕获存储过程的输出。

注意:语法是伪语法,但我希望你明白了!

这样,您就可以丢弃这些结果 - 因为这就是您的意图,而不会影响调用存储过程的输出!

但是,如果这些存储过程在您的控制之下,您应该考虑是否真的想重用它们/更改为仅返回输出参数而不返回结果集。您不必要地返回您不会使用的数据,至少在这种情况下是这样

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文