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

时间:2016-05-13 07:44:30

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

我做了一个查询来填充我的表并将其转换为存储过程。

CREATE PROCEDURE [dbo].[sp_getSAPCMTX]

@sSDate varchar(10), 
@sEDate varchar(10), 
@sBranch varchar(4000)

AS

DELETE FROM SAPCMTX 
WHERE DocDate BETWEEN @sSDate AND @sEDate AND
StoreCode IN (SELECT Item FROM dbo.SplitStrings(@sBranch , ',')) 

INSERT INTO SAPCMTX
SELECT * FROM Table T0
WHERE T0.DocDate BETWEEN @sSDate AND @sEDate AND T0.DataSource<>'O' AND 
  T1.TreeType<>'I' AND T0.DocType = 'I' AND 
  StoreCode IN COLLATE DATABASE_DEFAULT IN (SELECT Item FROM
  dbo.SplitStrings(@sBranch , ',')) 
GO

要运行它,我使用了这个命令

EXEC    [dbo].[sp_getSAPCMTX]
        @sSDate = '3/8/2016',
        @sEDate = '3/8/2016',
        @sBranch = '002'

它返回33行,但是当我尝试将它作为TSQL单独运行时,它返回34行。

DECLARE @sSDate varchar(10)
DECLARE @sEDate varchar(10) 
DECLARE @sBranch varchar(4000)

SET @sSDate = '3/8/2016'
SET @sEDate = '3/8/2016'
SET @sBranch = '002'


DELETE FROM SAPCMTX 
WHERE DocDate BETWEEN @sSDate AND @sEDate AND
StoreCode IN (SELECT Item FROM dbo.SplitStrings(@sBranch , ',')) 

INSERT INTO SAPCMTX
SELECT * FROM Table T0
WHERE T0.DocDate BETWEEN @sSDate AND @sEDate AND T0.DataSource<>'O' AND 
  T1.TreeType<>'I' AND T0.DocType = 'I' AND 
  StoreCode IN COLLATE DATABASE_DEFAULT IN (SELECT Item FROM
  dbo.SplitStrings(@sBranch , ',')) 
GO

任何想法为什么结果存在差异,因为它基本上是相同的查询?

1 个答案:

答案 0 :(得分:0)

这里有两种可能有助于您获得所需结果的可能性:

    在改变程序之前
  • SET ANSI_NULLS ON

    SET ANSI_NULLS ON
    GO
    ALTER PROCEDURE [dbo].[sp_getSAPCMTX]
        -- your code of the procedure
    
  • 创建本地属性并将参数值复制到其中:

    ALTER PROCEDURE [dbo].[sp_getSAPCMTX]
        @sSDate VARCHAR(10)
        @sEDate VARCHAR(10)
        @sBranch VARCHAR(4000)
    AS
    DECLARE @sDate VARCHAR(10)
    DECLARE @eDate VARCHAR(10)
    DECLARE @branch VARCHAR(4000)
    
    SET @sDate = @sSDate
    SET @eDate = @sEDate
    SET @branch = @sBranch
    
    -- your code of the procedure