简体   繁体   中英

Stored procedure call error in SQL Server 2008 R2

I have a stored procedure in my SQL Server that starts with these lines code:

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

-- my code...

I try to call the stored procedure using this code:

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

but this causes this error:

Msg 102, Level 15, State 1, Line 10
Syntax error near 'SP_Notify'.

You need to add EXEC before the stored procedure call:

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

Try:

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

EXEC SP_Notify @Username = @Username, @lastDate = @data

I've added EXEC , and specified the parameter values rather than relying on ordinal position to pass them in (this can come back to bite you otherwise, perhaps if you modify the SP in future, re-ordering and/or adding parameters).

使用exec SP_Notify @Username,@data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM