pytds调用的MSSQL存储过程返回的记录过多

时间:2019-04-24 17:36:26

标签: python sql-server python-3.x sql-server-2008

我在MSSQL数据库中有一个存储过程,如下所示:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[VideoSearchThorough]
(
  @SearchString NVARCHAR(4000) = ''
  , @Offset INT = 0
  , @Limit INT = 100
  , @UserId INT = 0
  , @ProviderId INT = NULL
  , @StatusType INT = 0
  , @StartDate datetime = NULL
  , @EndDate datetime = NULL
  , @UseUpdatedDate bit = 0
  , @PltValue INT = NULL
)
AS
BEGIN
    DECLARE @OrderClause VARCHAR(200) = 'CreatedDate DESC'

    IF @Limit IS NULL OR @Limit > 100 SET @Limit = 100

    CREATE TABLE #tmpsrch(
      VideoId BIGINT,
      UserId INT,
      VideoUrl VARCHAR(500),
      Title VARCHAR(1000),
      Description VARCHAR(5000),
      CreatedDate datetime,
      EffectiveDate datetime,
      ExpirationDate datetime,
      Height INT,
      Width INT,
      Duration decimal(10, 2),
      ThumbnailUrl VARCHAR(200),
      Keywords VARCHAR(200),
      IsEnabled bit,
      IsDeleted bit,
      ProviderId INT,
      ProviderName VARCHAR(200),
      ProviderLogo VARCHAR(200),
      ProviderTrackingGroup INT,
      ProviderIsMediaSource bit,
      ProviderContentIsPrivate bit
    )

    INSERT INTO #tmpsrch(VideoId, UserId, VideoUrl, Title, Description, CreatedDate, EffectiveDate, ExpirationDate, Height, Width, Duration, ThumbnailUrl, Keywords, IsEnabled, IsDeleted, ProviderId, ProviderName, ProviderLogo, ProviderTrackingGroup, ProviderIsMediaSource, ProviderContentIsPrivate)
    EXEC VideoSearch @SearchString=@SearchString, @MatchType=0

    IF (SELECT COUNT(*) FROM #tmpsrch (NOLOCK)) = 0
    INSERT INTO #tmpsrch(VideoId, UserId, VideoUrl, Title, Description, CreatedDate, EffectiveDate, ExpirationDate, Height, Width, Duration, ThumbnailUrl, Keywords, IsEnabled, IsDeleted, ProviderId, ProviderName, ProviderLogo, ProviderTrackingGroup, ProviderIsMediaSource, ProviderContentIsPrivate)
    EXEC VideoSearch @SearchString=@SearchString, @MatchType=1

    IF (SELECT COUNT(*) FROM #tmpsrch (NOLOCK)) = 0
    INSERT INTO #tmpsrch(VideoId, UserId, VideoUrl, Title, Description, CreatedDate, EffectiveDate, ExpirationDate, Height, Width, Duration, ThumbnailUrl, Keywords, IsEnabled, IsDeleted, ProviderId, ProviderName, ProviderLogo, ProviderTrackingGroup, ProviderIsMediaSource, ProviderContentIsPrivate)
    EXEC VideoSearch @SearchString=@SearchString, @MatchType=2

    SELECT TOP (@Limit) *, (SELECT COUNT(*) FROM #tmpsrch (NOLOCK)) AS ResultsAvailable FROM (
        SELECT
            ROW_NUMBER() OVER (ORDER BY CreatedDate DESC) AS rowId,
            *
        FROM #tmpsrch
    ) AS results
    WHERE results.rowId > @Offset

    DROP TABLE #tmpsrch
END

GO

在我的python 3代码中,我通过pytds模块调用此存储过程:

with pytds.connect(db_server, db_name, db_user, db_pass) as connection, connection.cursor() as cursor:
    cursor.callproc('VideoSearchThorough', ['search string', 0, 20])
    response = cursor.fetchall()
    result = []
    for rowData in response:
        row = {}
        for columnIndex, columnData in enumerate(cursor.description):
            row[columnData[0]] = rowData[columnIndex]
        result.append(row)

    return result # This contains 84 result records

但是,当我在SQL Operations Studio客户端中执行以下SQL时:

VideoSearchThorough 'search string', 0, 20

..仅返回20条记录。我只需要20条记录,因为我将20作为限制(提供给VideoSearchThorough存储过程的第三个参数)。

但是,在Python 3.7中使用pytds模块时,总共返回了84条记录,为什么会有区别?

1 个答案:

答案 0 :(得分:1)

我在您的Python代码中看不到您在设置@Limit变量:如果您未设置该变量,则使用默认值100,但只有87条记录要返回。