尝试在过程中执行过程时出错

Getting an error when trying to execute procedure within a procedure

我正在尝试编写一个在其中执行另一个过程的过程,但出现此错误

Incorrect syntax near '@PRIMARY_AM'

但我仅在具有 CAST() OR ISNULL() OR REPLACE() 函数的变量中出现此错误。如果我用 @PRIMARY_AM 注释掉那一行,它会说

Incorrect syntax near '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

这就是我的问题的解决方案,感谢 Alex 和他在我的问题下的评论

Stored procedures, accept parameters (variables and constants) but not expressions. You need to do all casting before calling your SP. – Alex

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;