我正在尝试编写一个在其中执行另一个过程的过程,但出现此错误
“ @ PRIMARY_AM”附近的语法不正确
但是只有具有CAST()
或ISNULL()
或REPLACE()
函数的变量才会出现此错误。如果我用@PRIMARY_AM
注释掉该行,然后会说
“ ISNULL”附近的语法不正确
CREATE PROCEDURE [dbo].[RUN_PROCESS]
@PRIMARY_NO VARCHAR(20) = NULL,
@COMBINED_AM INT = NULL,
@PRIMARY_BOR VARCHAR(40) = NULL,
@PRIMARY_AM INT = NULL,
@SECONDARY_AM INT = NULL,
@SECONDARY_DT SMALLDATETIME = NULL,
@PRIMARY_CD VARCHAR(10) = NULL,
@O_ID INT OUTPUT
AS
EXEC dbo.LINK_PROCESS
@PRIMARY_NO = @PRIMARY_NO,
@COMBINED_AM = CAST(@PRIMARY_AM + @SECONDARY_AM AS VARCHAR),
@PRIMARY_BOR = REPLACE(ISNULL(@PRIMARY_BOR, ''), '''',' '),
@PRIMARY_AM = CAST(ISNULL(@PRIMARY_AM, 0) AS VARCHAR),
@SECONDARY_AM = CAST(ISNULL(@SECONDARY_AM, 0) AS VARCHAR),
@SECONDARY_DT = CAST(@SECONDARY_DT AS VARCHAR),
@PRIMARY_CD = ISNULL(@PRIMARY_CD, ''),
@O_ID = @O_ID OUTPUT;
如果我删除了这些功能CAST()
,REPLACE()
和ISNULL()
,那么我可以毫无问题地执行查询。我不知道为什么这行不通。
我正在使用SQL Server 2005
答案 0 :(得分:2)
这是我的问题的解决方案,这要感谢亚历克斯(Alex)及其在我的问题下的评论
存储过程,接受参数(变量和常量),但不接受 表达式。您需要在调用SP之前进行所有投射。 –亚历克斯
CREATE PROCEDURE [dbo].[RUN_PROCESS]
@PRIMARY_NO VARCHAR(20) = NULL,
@COMBINED_AM INT = NULL,
@PRIMARY_BOR VARCHAR(40) = NULL,
@PRIMARY_AM INT = NULL,
@SECONDARY_AM INT = NULL,
@SECONDARY_DT SMALLDATETIME = NULL,
@PRIMARY_CD VARCHAR(10) = NULL,
@O_ID INT OUTPUT
AS
DECLARE @COMBINED VARCHAR
SET @COMBINED = CAST(@PRIMARY_AM + @SECONDARY_AM AS VARCHAR)
DECLARE @PRIM_BOR VARCHAR(40)
SET @PRIM_BOR = REPLACE(ISNULL(@PRIMARY_BOR, ''), '''',' ')
DECLARE @PRIM_AM VARCHAR
SET @PRIM_AM = CAST(ISNULL(@PRIMARY_AM, 0) AS VARCHAR)
DECLARE @SEC_AM VARCHAR
SET @SEC_AM = CAST(ISNULL(@SECONDARY_AM, 0) AS VARCHAR)
DECLARE @SEC_DT VARCHAR
SET @SEC_DT = CAST(@SECONDARY_DT AS VARCHAR)
DECLARE @PRIM_CD VARCHAR(10)
SET @PRIM_CD = ISNULL(@PRIMARY_CD, '')
EXEC dbo.LINK_PROCESS
@PRIMARY_NO = @PRIMARY_NO,
@COMBINED_AM = @COMBINED,
@PRIMARY_BOR = @PRIM_BOR,
@PRIMARY_AM = @PRIM_AM,
@SECONDARY_AM = @SEC_AM,
@SECONDARY_DT = @SEC_DT,
@PRIMARY_CD = @PRIM_CD,
@O_ID = @O_ID OUTPUT;