3

我有一个存储过程,它调用 2 个其他存储过程来获取一些值。

ALTER PROCEDURE [dbo].[Delivery_SelectByJobID] 
(
    @job_ID int
)
as
-- if possible - if count(pods) > 0 return true
-- and           if count(photos) > 0 return true
declare @podCount int = 0;
declare @photoCount int = 0;

EXEC @podCount=countPodsForJob @job_ID
EXEC @photoCount =countPhotosForJob @job_ID

declare @podsBit bit=0
declare @photosBit bit=0

if @podCount > 0
    begin
set @podsBit = 1
    end

if @photoCount > 0
    begin
    set @photosBit = 1
    end

SELECT     Job_Reference_No, Job_Start_Date, Job_POD_Filename, Job_Photo_Filename, 
Job_Signed_For_Name, Job_ID, Job_Status, Job_Delivery_Notes, @podsBit as Job_POD_Supplied, 
                      @photosBit as Job_Photo_Supplied
FROM         Jobs
WHERE     (Job_ID = @Job_ID) AND (Job_Status <> 7)

当我运行它时,它返回 3 组结果,即 Count_pods、Count_Photos 和选择的结果。我只想返回选择的结果。如何停止返回其他结果?

4

3 回答 3

1

Solved this.

I needed to convert countPodsForJob and countPhotosForJob to functions that returned values rather than results set.

于 2013-10-16T19:42:37.977 回答
0

您的存储过程countPodsForJobcountPhotosForJob返回它们自己的结果集。如果传递了特定参数,您可以重写它们以不执行 SELECT,或者(更好的选择)将它们的替代项创建为标量 UDF。

另一种选择(如果您无法控制现有 SP 并且无法创建新函数)很容易忽略调用方应用程序中的其他结果集。(例如,在 ADO.NET DataSet 中,您可以简单地获取第三个 DataTable)。

于 2013-10-16T19:35:26.087 回答
0

您可以使用

SET NOCOUNT ON;    ---- after the AS in your proc's Definition.

它将停止发送消息为 12 行......

检查其他存储过程的定义并对它们做同样的事情.....
事实上我刚刚注意到你的嵌套存储过程和变量@prodCount 和@PhotoCount 的返回值使它们成为输出参数,这将存储值在它们中传递参数,然后您可以将它们传递 OUT 参数给您的 SELECT 语句。

一旦你改变了嵌套过程的定义,你可以做这样的事情..

 ALTER PROCEDURE [dbo].[Delivery_SelectByJobID] 
    (
        @job_ID int
    )
    as
    SET NOCOUNT ON;
    -- if possible - if count(pods) > 0 return true
    -- and           if count(photos) > 0 return true
    declare @podCount int = 0;
    declare @photoCount int = 0;

    --EXEC @podCount=countPodsForJob @job_ID          --Your Code
    --EXEC @photoCount =countPhotosForJob @job_ID     --Your Code

    EXEC countPodsForJob @job_ID, @podCount OUTPUT         --My Suggestion
    EXEC countPhotosForJob @job_ID, @photoCount OUTPUT     --My Suggestion

    declare @podsBit bit=0
    declare @photosBit bit=0

    if @podCount > 0
        begin
    set @podsBit = 1
        end

    if @photoCount > 0
        begin
        set @photosBit = 1
        end

    SELECT     Job_Reference_No, Job_Start_Date, Job_POD_Filename, Job_Photo_Filename, 
    Job_Signed_For_Name, Job_ID, Job_Status, Job_Delivery_Notes, @podsBit as Job_POD_Supplied, 
                          @photosBit as Job_Photo_Supplied
    FROM         Jobs
    WHERE     (Job_ID = @Job_ID) AND (Job_Status <> 7)
于 2013-10-16T19:04:11.657 回答