SQL作为查询工作,但在作为存储过程调用时失败

时间:2018-02-06 13:26:09

标签: sql sql-server stored-procedures

我有这个SQL,它正在运行:

WITH MyTable AS
(
    SELECT RegistrationID, ID,
    (ROW_NUMBER() OVER ( ORDER BY RegistrationID )) AS 'RowNumber'
    FROM tblData_Visit
            WHERE RegistrationID = @baseid
) 
SELECT ' ' + CAST(RowNumber AS nvarchar) AS 'Row#'     
FROM MyTable 
WHERE RegistrationID = @baseid AND ID = @objectid

当我创建存储过程时,

CREATE PROC [dbo].[upMeta_VisitIndex]
(
@baseid int,
@objectid int
)
AS
WITH MyTable AS
(
    SELECT RegistrationID, ID,
        (ROW_NUMBER() OVER ( ORDER BY RegistrationID )) AS 'RowNumber'
    FROM tblData_Visit
    WHERE RegistrationID = @baseid
) 
SELECT ' ' + CAST(RowNumber AS nvarchar) AS 'Row#'     
FROM MyTable 
WHERE RegistrationID = @baseid AND ID = @objectid


GO

,并使用以下方式调用:

EXEC upMeta_VisitIndex @baseid @objectid

我最终在errorville:

SQL exception data:
SQL error number: 102. SQL state:1. Error class: 15. Message: "Incorrect 
syntax near '@objectid'.". Procedure:"".
System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near 
'@objectid'.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, 
Boolean breakConnection, Action`1 wrapCloseInAction)...............

鉴于,我知道@baseid和@objectid不为空,我认为问题在于我如何调用SP。这里有什么明显的东西我不见了吗?

干杯, GAV。

2 个答案:

答案 0 :(得分:5)

调用存储过程时,您似乎缺少参数之间的逗号。试试这个:

EXEC upMeta_VisitIndex @baseid , @objectid

答案 1 :(得分:3)

在调用存储过程时,您在EXEC语句中缺少逗号

EXEC upMeta_VisitIndex @baseid, @objectid