存储过程问题:“参数太多”

时间:2013-10-24 12:20:09

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

这似乎是一个相当普遍的问题,我正在摸索为什么会发生这种情况。我正在使用一个软件应用程序,我无法直接访问源代码(我只能使用.NET Reflector查看)。

存储过程如下:

ALTER PROCEDURE [dbo].[CS_GetMessages] (
    @CustomerID C_ID, @Level C_Integer, @UsersTimeZoneOffset C_Integer
    )
AS
BEGIN 
    SET NOCOUNT ON
    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

    DECLARE @Date Datetime
    SELECT @Date = CONVERT(nvarchar(10), getutcdate(), 101)

    SELECT
        [MessageID]

    FROM
        [dbo].[Messages]
    WHERE
        ([CustomerID] = @CustomerID OR [CustomerID] IS NULL) 
        AND Enabled = 1
        AND Deleted = 0
        AND (EffectiveDate IS NULL OR @Date >= CONVERT(nvarchar(10), DATEADD(minute, @UsersTimeZoneOffset, EffectiveDate), 101))
        AND (ExpiryDate IS NULL OR @Date < CONVERT(nvarchar(10),  DATEADD(minute, @UsersTimeZoneOffset, ExpiryDate), 101))
        AND Publish = 1
        AND Level = ISNULL(@Level, Level)
    ORDER BY MaintenanceDate DESC

END

现在,根据源代码(遗憾的是由于授权我无法粘贴它),它会发送适当数量的参数。 SQL事件探查器也正确地显示了这一点:

exec CS_GetMessages @CustomerID=N'CT000001',@Level=NULL,@UsersTimezoneOffset=0

然而,它抛出一个错误:

Error: 8144, Severity: 16, State: 2 Procedure or function CS_GetMessages has too many arguments specified.

但是,如果我手动运行该过程,它会正确返回一个值。我使用了以下测试代码:

DECLARE @return_value int

EXEC    @return_value = [dbo].[CS_GetMessages]
        @CustomerID = 'CT000001',
        @Level = NULL,
        @UsersTimeZoneOffset = 0

SELECT  'Return Value' = @return_value

GO

请注意,由于数据库是空白的(空表),因此没有数据,所以它适当地返回0.对于可能发生的事情的任何想法?

UPDATE:执行数据库调用的源代码:

 private SystemMessageManager LoadSystemMessages()
        {
            int paramValue = Conversions.ToInteger(Utility.DataFromSession("UsersTimezoneOffset", string.Empty));
            SystemMessageManager messageManager = new SystemMessageManager();
            CriteriaCollection loadCriteria = new CriteriaCollection();
            CriteriaCollection criterias2 = loadCriteria;
            criterias2.Add("CustomerID", DbType.String, Utility.GetCurrentCustomer().CustomerID, ParameterDirection.Input, "");
            criterias2.Add("Level", DbType.Int32, null, ParameterDirection.Input, "");
            criterias2.Add("UsersTimezoneOffset", DbType.Int32, paramValue, ParameterDirection.Input, "");
            criterias2 = null;
            messageManager.Load(loadCriteria);
            this.CheckForDeactivation(messageManager);
            return messageManager;
        }

1 个答案:

答案 0 :(得分:1)

ALTER PROCEDURE [dbo]。[CS_GetMessages](     @CustomerID C_ID,@ Level C_Integer,@ UserTimeZoneOffset C_Integer     )

将您的代码更改为

ALTER PROCEDURE [dbo].[CS_GetMessages] (
    @CustomerID BigInt, @Level Integer=NULL, @UsersTimeZoneOffset Integer
    )

默认情况下,整数为0而不为空。所以你需要专门将其设为null。

exec CS_GetMessages @CustomerID=1,@Level=NULL,@UsersTimezoneOffset=0