如何指定要使用的存储过程结果集?

时间:2014-06-12 19:46:39

标签: c# sql

我有一个调用存储过程的C#类,如下所示:

public static ReadOnlyCollection<IDataRow> GetDBSData(Int32 myPeriod, DateTime myWEDate)
{
    string dateFormat = "MM/dd/yyyy";
    string query = string.Format(
        "exec GatherDBSData '{0}', '{1}'",
        myPeriod.ToString(),
        myWEDate.ToString(dateFormat)
    );
    return FileGenerator.GetDataRows(query, DBSMeta.Columns, dict => new DBSDataRow(dict));
}

我遇到的挑战是GatherDBSData sproc调用另一个sproc并且我得到两个结果集。我想要的只是最后的结果集。 GatherDBSData包含此调用...

exec GatherPaperlessPayPayrollData @Period, @CheckDate

......作为它的第一件事。上面的调用会在我的GatherDBSData sproc所需的DB中填充一个表,以便完成其工作。

我的问题是 - 有没有办法调用GatherDBSData并指示它只返回第二个结果集?或者我可以在GatherDBSData中调用GatherPaperlessPayPayrollData并告诉它我不想查看或使用结果集,因此只返回一个?

2 个答案:

答案 0 :(得分:0)

如果要指示存储过程仅返回一个结果集,则可以向其添加一个新参数,以便指示要返回的结果集数量。

如果您只想跳过返回的第一个结果集,可以使用SqlDataReader.NextResult()http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.nextresult.aspx来获取第二个结果集。

答案 1 :(得分:0)

我第二次遇到了这个完全相同的问题,用于初始发布此问题的解决方法在第二种情况下无效。

在方案#2中,我有一个调用存储过程的SSRS报告。该存储过程调用另一个存储过程,最后,两个结果集都被返回,并且报告尝试使用第一个结果集,而不是第二个。我只需要第二个结果集。那里有 IS 解决方案。

这个问题的解决方案是dyslexicanaboko在回答这个问题时采取的一种非常有创意的方法:How to Suppress the SELECT Output of a Stored Procedure called from another Stored Procedure in SQL Server?虽然dyslexicanaboko的提议不是该用户案例中的选定答案,但事实证明它非常有效。

通过将可选参数添加到我的报告的sproc调用的存储过程中,我可以抑制我不想要的结果集的输出。只有第二个结果集被送入报告。

新的可选参数:

ALTER PROCEDURE [dbo].[GatherPaperlessPayPayrollData] 
(
    @Period int,
    @CheckDate datetime,
    @Employee  VARCHAR(20) = NULL,
    @silentExecution bit = 0
)

改变存储过程的最后一行,如下所示:

IF @silentExecution = 0
     SELECT * FROM PaperlessPay;

将对存储过程的调用改为:

if @custEID is not null
    begin
        exec InterfaceDev..GatherPaperlessPayPayrollData @Period, @CheckDate, @custEID, @silentExecution = 1
    end
else 
    begin
        exec InterfaceDev..GatherPaperlessPayPayrollData @Period, @CheckDate, @silentExecution = 1
    end