使用SET NOCOUNT ON会影响从存储过程返回的结果值

时间:2013-07-18 13:34:18

标签: sql sql-server

为StoredProcedure提供以下脚本..

CREATE PROCEDURE [dbo].[GetInvestorForExtractReport]
    -- Add the parameters for the stored procedure here
    @rptPrm_AccountNumber varchar(8000),
    @SettlmentDate as datetime
with encryption
AS

    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT distinct
        I.InvestorID, 
        I.InvestorName As [Nombre Inversionista],
        substring(IA.AccountNumber,1,3) accountnumber,
        isnull(AGSL.CashGuarantee,0) As [Garantia Efectivo],
        I.DocumentType As [Tipo Documento],
        I.DocumentNumber As [Numero Documento],
        substring(I.MainAccount,1,3) As [Cuenta CRCC],
        I.InternalInvestorNumber As [Cuenta Interna],
        SUBSTRING(CONVERT(VARCHAR,getdate(),120),1,10) As [Fecha],
        isnull(AGSL.InvestorCashGuarantee,0) As [Efectivo]
    FROM 
        Investors I
        INNER JOIN investoraccounts IA 
            ON (I.InvestorId = IA.InvestorId AND IA.AccountNumber like '%01' AND
            (substring(IA.AccountNumber,1,3) IN (select param from  dbo.fn_ReportParams(@rptPrm_AccountNumber,','))))
        LEFT JOIN AccountGuaranteeStatusLog AGSL
            ON (AGSL.InvestorAccount = substring(IA.AccountNumber,1,3)
                AND datediff(dd,AGSL.SettlmentDate ,@SettlmentDate)=0)
    order by substring(IA.AccountNumber,1,3)
GO

如果删除 SET NOCOUNT ON ,并且使用相同的参数执行存储过程,则列[Efectivo]的值会发生变化!怎么可能?

2 个答案:

答案 0 :(得分:0)

如果您尝试在SSMS中运行存储过程,则无论NOCOUNT是ON还是OFF,数据结果都应相同。如果您发现结果确实不同,那么这可能是您行动时间的巧合。

不同之处在于,您将从存储过程中获得以下内容: (494行(s)受影响)

当从程序执行此操作时,通常会使用此值返回第二个返回的数据集。

答案 1 :(得分:0)

我不知道你是否解决了这个问题,但是如果你修改下面的sp,你能不能告诉我你是否仍然遇到同样的问题?

CREATE PROCEDURE [dbo].[GetInvestorForExtractReport]
-- Add the parameters for the stored procedure here
@rptPrm_AccountNumber varchar(8000),
@SettlmentDate as datetime

加密 AS

-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here
SELECT distinct
    I.InvestorID, 
    I.InvestorName As [Nombre Inversionista],
    substring(IA.AccountNumber,1,3) accountnumber,
    isnull(AGSL.CashGuarantee,0) As [Garantia Efectivo],
    I.DocumentType As [Tipo Documento],
    I.DocumentNumber As [Numero Documento],
    substring(I.MainAccount,1,3) As [Cuenta CRCC],
    I.InternalInvestorNumber As [Cuenta Interna],
    SUBSTRING(CONVERT(VARCHAR,getdate(),120),1,10) As [Fecha],
    isnull(AGSL.InvestorCashGuarantee,0) As [Efectivo]
FROM 
    Investors I
    INNER JOIN investoraccounts IA 
        ON (I.InvestorId = IA.InvestorId AND IA.AccountNumber like '%01' AND
        (substring(IA.AccountNumber,1,3) IN (select param from  dbo.fn_ReportParams(@rptPrm_AccountNumber,','))))
    LEFT JOIN AccountGuaranteeStatusLog AGSL
        ON (AGSL.InvestorAccount = substring(IA.AccountNumber,1,3))
            where datediff(dd,AGSL.SettlmentDate ,@SettlmentDate)=0
order by substring(IA.AccountNumber,1,3)