当我通过应用程序执行存储过程时,SQL Server会引发超时,但在Management Studio中执行存储过程时不会引发超时

当我通过应用程序执行存储过程时,SQL Server会引发超时,但在Management Studio中执行存储过程时不会引发超时,sql,sql-server,node.js,Sql,Sql Server,Node.js,我有这样一个存储过程: PROCEDURE [dbo].[myStoredProcedure] @period varchar(7), @pgSize int, @pg int, @sort varchar(50) AS BEGIN DECLARE @exec nvarchar(1000) DECLARE @order varchar(100) DECLARE @where varchar(100) CREATE TABLE #temp (

我有这样一个存储过程:

PROCEDURE [dbo].[myStoredProcedure]
 @period varchar(7),
 @pgSize int,
 @pg int,
 @sort varchar(50)
AS
BEGIN
    DECLARE @exec nvarchar(1000)
    DECLARE @order varchar(100)
    DECLARE @where varchar(100)

    CREATE TABLE #temp (
        fieldOne varchar(7),
        fieldTwo varchar(7),
        fieldThree int,
        fieldFour varchar(7)
    )   

    If exists(select 1 from tableA Where date = @period)
    Begin
        INSERT INTO #temp
        SELECT DISTINCT a.fieldOne, b.fieldTwo, td.fieldThree, b.fieldFour
        FROM tableA a
        LEFT JOIN docType td ON td.code = a.docType
        INNER JOIN tableB b ON a.code = b.code
        AND b.state= 'A'
        AND a.date = convert(int, @period);
    END
    ELSE
    BEGIN
        INSERT INTO #temp
        SELECT DISTINCT a.fieldOne, b.fieldTwo, td.fieldThree, b.fieldFour
        FROM tableAHistoric a
        LEFT JOIN docType td ON td.code = a.docType
        INNER JOIN tableBHistoric b ON a.code = b.code
        AND b.state= 'A'
        AND a.date = convert(int, @period);
    END

    -- Eliminate registers such that there is another register with the same fieldOne but different fieldTwo, and keep the one that has smaller fieldThree or,
    -- in case they are equal, keep the one with smallest fieldFour
    DELETE t1
    FROM #temp t1
    INNER JOIN #temp t2 ON t1.fieldOne = t2.fieldOne AND t1.fieldTwo <> t2.fieldTwo
    WHERE t1.fieldThree > t2.fieldThree OR (t1.fieldThree = t2.fieldThree AND t1.fieldFour > t2.fieldFour)

    SET @order = ' order by ' + CASE LEFT(@sort, 1) WHEN '-' THEN SUBSTRING(@sort, 2, LEN(@sort) - 1) + ' desc' ELSE @sort END 

    IF @pg = 1
    BEGIN
        SET @exec = CONCAT(
                'select * from #temp ', 
                @order, 
                ' offset (@pg-1) * @pgSize rows  fetch next @pgSize rows only '
                )

        EXEC sp_executesql @exec, N'@pg int, @pgSize int', @pg = @pg, @pgSize = @pgSize
        SELECT rows = COUNT(*) FROM #temp
    END
    ELSE
    BEGIN
        SET @exec = CONCAT(
                'select * from #temp ', 
                @order, 
                ' offset (@pg-1) * @pgSize rows  fetch next @pgSize rows only '
                )
        EXECUTE sp_executesql @exec, N'@pg int, @pgSize int', @pg = @pg, @pgSize = @pgSize
    END 
它在一秒钟内返回

如果我在变量@period中硬编码“201603”,并从Node.js应用程序执行存储过程,它也会在一秒钟内返回

但是,如果在通过Node.js应用程序执行存储过程时传递该参数,则会得到一个超时。我已经注册,存储过程要么永远无法执行INSERT,要么永远无法在超时之前完成。超时设置为15秒,我将其设置为2分钟进行测试,但结果是一样的:超时。有趣的是,它只发生在值“201603”的情况下。结果应返回889个寄存器。我已经用其他返回200个结果的值进行了测试,它们没有问题

作为参考,字段日期为整数。但即使我在WHERE子句中转换参数@period,也不会有任何变化

有什么问题吗?我应该调查什么


谢谢。

可能是缓存的执行计划


尝试在SP的底部添加选项“重新编译”

进程中没有@period用作VARCHAR,那么为什么要将其声明为VARCHAR呢?如果你只把它作为一个INT,你应该把它声明为INT。@Siyual:虽然这是一个很好的观点,但我发现把@period作为INT传递给这个问题没有任何区别。
EXEC myStoredProcedure '201603', 10, 1, 'fieldOne'