0

我正在尝试在 SQL Server 2008 上创建存储过程,但出现错误:

消息 156,级别 15,状态 1,过程 usp_UpdateDistribiutionList,第 12 行关键字“AS”附近的语法不正确。

你能建议吗?

存储过程代码:

USE LogDB
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE usp_UpdateDistribiutionList 
-- Add the parameters for the stored procedure here
@distId INT,
@DistEmails varchar(max)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
UPDATE [NotificationDistribution] AS dist
SET dist.emailCC = @DistEmails
WHERE dist.DistributionID = @distId
END

第二个问题:

是否可以使用参数来获取像 dist.@columnName 这样的列名?

4

2 回答 2

3

删除语句AS中的UPDATE,并将其放在FROM子句中,它应该是:

UPDATE dist
SET dist.emailCC = @DistEmails
FROM [NotificationDistribution]  AS dist
WHERE dist.DistributionID = @distId

对于您的第二个问题,您不能这样做,您必须使用动态 SQL 动态地执行此操作。

于 2013-09-08T10:23:21.903 回答
1

将您的更新更改为以下内容:

UPDATE [NotificationDistribution]
SET emailCC = @DistEmails
WHERE DistributionID = @distId

如果你想做动态:

CREATE PROCEDURE usp_UpdateDistribiutionList 
-- Add the parameters for the stored procedure here
@distId INT,
@DistColumn varchar(max),
@DistValue varchar(max)
AS
BEGIN


    SET NOCOUNT ON;

    DECLARE @SQL AS NVARCHAR(MAX)

    SET @SQL = 
    N'UPDATE [NotificationDistribution]
    SET ' + QuoteName(@DistColumn ) + ' = @DistValue 
    WHERE DistributionID = @distId'

    EXECUTE sp_executesql 
    @SQL,
    N'@DistValue  varchar(max), @distId INT',
    @DistValue  = @ColumnValue,
    @distId = @distId

END
于 2013-09-08T10:23:39.683 回答