存储过程的结果与SSMS查询不同

时间:2016-04-21 11:03:18

标签: sql sql-server sql-server-2008 stored-procedures

我在存储过程和查询窗口中有相同的代码,但是我得到了不同的结果。唯一的区别是SP正在选择临时表,然后从临时表中进行选择,并且它正在SP中运行。

SP没有参数 - 所以它不是参数嗅探,SP有ANSI_NULLS ON。

RecId和invoiceOrderRecId是bigints,品牌是nvarchar(10)

  SELECT 
      ZIOH.Brand, 
      Count(RecId) as NumDispatched
  FROM 
      ZFSINVOICEORDERHISTORY ZIOH
  WHERE 
      ZIOH.DISPATCHDATETIME  >=  CAST(CURRENT_TIMESTAMP AS DATE)
      AND 
      ZIOH.DISPATCHDATETIME < DATEADD(DD, 1, CAST(CURRENT_TIMESTAMP AS DATE))
      AND 
      ZIOH.INVOICEORDERRECID NOT IN 
                              (SELECT 
                                   RecId 
                               FROM
                                   ZFSINVOICEORDER ZIO
                               WHERE
                                   ZIO.PARTITION = ZIOH.PARTITION)
   GROUP BY ZIOH.Brand
   ORDER BY ZIOH.Brand

查询窗口的结果

╔═══════╦═══════════════╗
║ Brand ║ NumDispatched ║
╠═══════╬═══════════════╣
║ CCO   ║             1 ║
║ CVDUK ║            13 ║
║ FLX   ║            12 ║
║ MSFR  ║            74 ║
║ MSGR  ║             1 ║
║ MSUK  ║           211 ║
║ PIAFR ║            25 ║
║ PIAUK ║           129 ║
╚═══════╩═══════════════╝

SP的结果

╔═══════╦═══════════════╗
║ Brand ║ NumDispatched ║
╠═══════╬═══════════════╣
║ CCO   ║             1 ║
║ CVDUK ║             7 ║
║ FLX   ║            12 ║
║ MSFR  ║            53 ║
║ MSUK  ║           147 ║
║ PIAFR ║            21 ║
║ PIAUK ║           121 ║
╚═══════╩═══════════════╝

1 个答案:

答案 0 :(得分:2)

未提交的交易导致结果出现差异 - 这是早上失去的。