【问题标题】:Stored procedure call error in SQL Server 2008 R2SQL Server 2008 R2 中的存储过程调用错误
【发布时间】:2012-11-29 14:07:15
【问题描述】:

我的 SQL Server 中有一个以以下代码开头的存储过程:

CREATE PROCEDURE [dbo].[SP_Notify]
-- Add the parameters for the stored procedure here
@UserName nvarchar(50),
@lastDate datetime
AS
BEGIN

-- my code...

我尝试使用以下代码调用存储过程:

DECLARE @data datetime
DECLARE @Username nvarchar(50)
SET @Username = CAST('myUserName' AS nvarchar(50))
SET @data = GetDate()
SP_Notify @Username , @data

但这会导致此错误:

消息 102,第 15 级,状态 1,第 10 行
“SP_Notify”附近的语法错误。

【问题讨论】:

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


【解决方案1】:

需要在存储过程调用前加上EXEC

DECLARE @data datetime
DECLARE @Username nvarchar(50)
SET @Username = CAST('myUserName' AS nvarchar(50))
SET @data = GetDate()
EXEC SP_Notify @Username , @data

【讨论】:

    【解决方案2】:

    试试:

    DECLARE @data datetime
    DECLARE @Username nvarchar(50)
    SET @Username = CAST('myUserName' AS nvarchar(50))
    SET @data = GetDate()
    
    EXEC SP_Notify @Username = @Username, @lastDate = @data
    

    我添加了EXEC,并指定了参数值,而不是依赖于序数位置来传递它们(否则这可能会反过来咬你,也许如果你将来修改 SP,重新排序和/或添加参数)。

    【讨论】:

    • 最后一个参数应该是@lastDate = @data
    【解决方案3】:

    使用exec SP_Notify @Username,@data

    【讨论】:

      猜你喜欢
      • 2015-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-25
      • 1970-01-01
      • 1970-01-01
      • 2015-06-23
      • 2018-09-21
      相关资源
      最近更新 更多