Sql 使用SETNOCOUNT ON会影响存储过程返回的结果值
为StoredProcess使用以下脚本Sql 使用SETNOCOUNT ON会影响存储过程返回的结果值,sql,sql-server,Sql,Sql Server,为StoredProcess使用以下脚本 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
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]
的值将更改!这怎么可能 如果尝试在SSMS中运行存储过程,则无论NOCOUNT是打开还是关闭,数据结果都应相同。如果你发现结果确实不同,那么这可能是你行动时机上的巧合
不同之处在于,您将从存储过程中得到如下内容:
(受影响的494排)
当从程序执行此操作时,通常会返回第二个数据集,其中包含此值。我不知道您是否解决了此问题,但如果您像下面这样修改sp,您能告诉我您是否仍然遇到相同的问题吗
CREATE PROCEDURE [dbo].[GetInvestorForExtractReport]
-- Add the parameters for the stored procedure here
@rptPrm_AccountNumber varchar(8000),
@SettlmentDate as datetime
加密
作为
你确信一切都一样吗?相同的数据库、相同的数据(没有人更新基础表)和yes-完全相同的参数?为什么在两个不同的执行中该值应该相同?您的数据是只读的吗?是一个没有其他人更新的数据库,其中一个参数是过去的日期,在两次执行中都是固定的,基本上,如果我删除/添加SET NOCOUNT,则值会从0切换到正确的值,该值始终与正在设置的SET NOCOUNT相关,是否有任何触发器或其他后台进程可能会改变数据?@MauricioGracia因此,有无SET NOCOUNT ON-该列的值不同,但差异不大甜品?打开时总是相同的值,不打开时总是相同的值?正如我前面所说,没有其他人在修改数据库,所以我不认为这怎么可能是巧合。从SSMSI执行的所有测试都将在存储过程外部运行select语句,在使用相同参数运行相同存储过程之前或之后立即运行,并比较结果。您的Efectivo列似乎有一个ISNULL条件,这可能是您得到不同结果的原因吗?如果左连接没有返回任何记录,那么您可能会在其中得到一个空值。我确实从存储过程中取出了select,它返回了预期的值,也就是说,当我决定删除SET NOCOUNT ON时,存储过程开始显示正确的结果。但是我仍然不明白为什么那条语句会影响select的结果。完全没有理由认为SET NOCOUNT ON会对查询中结果集的数据产生任何影响。您可以尝试移动列顺序,看看这是否有影响,或者在末尾添加AGSL.investorCashGuarrance,看看列的实际值是多少。我也希望您的声明是正确的,因为这种行为真的很奇怪,这就是发布Question的原因(由于问题已通过删除SET NOCOUNT ON解决)通过删除“SET NOCOUNT ON”解决了问题声明。我将您发布的代码与原始代码进行了比较,结果是一样的。也许您发布了原始代码。这不一样,但我很高兴您解决了您的问题。我的代码中的关键区别在于包含了
datediff(dd,AGSL.settletmentdate,@settletmentdate)=0
where子句上的条件。使用外部联接时,请小心,仅将此类筛选条件放置在where子句.Radu上,但这如何解释相同的SELECT在存储过程内部的行为不同于外部?在这两种情况下,都使用了相同的where子句。我与记事本++进行了比较,发现两者不同ce未显示或未被我检测到,我将重试并让您知道
-- 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)