0

问题:如果我将 IF not exists 添加到创建过程作为外部名称语句,我会收到语法错误...为什么?如果我分别运行它们,这两个语句都可以正常工作......

IF  NOT EXISTS 
(
    SELECT * FROM sys.objects 
    WHERE object_id = OBJECT_ID(N'PriceSum') 
    AND type in (N'P', N'PC')
)
CREATE PROCEDURE PriceSum(@sum int OUTPUT)
     AS EXTERNAL NAME MyFirstUdp.[SQL_CLRdll.MySQLclass].PriceSum
4

2 回答 2

1

因为 CREATE PROCEDURE 必须是批处理中的第一条语句。

要么先删除 proc,要么使用动态 SQL

IF  NOT EXISTS 
(
    SELECT * FROM sys.objects 
    WHERE object_id = OBJECT_ID(N'PriceSum') 
    AND type in (N'P', N'PC')
)
exec('CREATE PROCEDURE PriceSum(@sum int OUTPUT)
     AS EXTERNAL NAME MyFirstUdp.[SQL_CLRdll.MySQLclass].PriceSum')

你也可以颠倒逻辑

    IF  EXISTS 
    (
        SELECT * FROM sys.objects 
        WHERE object_id = OBJECT_ID(N'PriceSum') 
        AND type in (N'P', N'PC')
    )

    DROP PROCEDURE PriceSum
    GO --this ends the batch, so the create statement below is fine

    CREATE PROCEDURE PriceSum(@sum int OUTPUT)
AS EXTERNAL NAME MyFirstUdp.[SQL_CLRdll.MySQLclass].PriceSum
  GO
于 2010-07-01T11:49:01.560 回答
0

尝试

IF  NOT EXISTS 
(
    SELECT * FROM sys.objects 
    WHERE object_id = OBJECT_ID(N'PriceSum') 
    AND type in (N'P', N'PC')
)

EXEC ('CREATE PROCEDURE PriceSum(@sum int OUTPUT)
     AS EXTERNAL NAME MyFirstUdp.[SQL_CLRdll.MySQLclass].PriceSum')

我不认为它喜欢之前CREATE PROCEDURE和同一批次的东西。

于 2010-07-01T11:48:21.907 回答