关键字'WHEN'附近的语法不正确

时间:2015-04-03 16:11:36

标签: sql-server stored-procedures

尝试创建存储过程以根据字段插入或更新记录。

CREATE PROCEDURE [dbo].[usp_InsertorUpdateDB]
  @dp_id                char(32),
  @dv_id                char(32),
  @em_number            char(12),
  @email                varchar(50),
  @emergency_relation char(32),
  @option1          char(16),
  @status           char(20),
  @em_id                char(35),
  @em_title         varchar(64),
  @date_hired           datetime

AS

 MERGE [dbo].[em] AS [Target]
 USING (SELECT @dp_id, @dv_id , @em_number, @email, @emergency_relation, @option1, @status, @em_id, @em_title, @date_hired)
 AS [Source] ([dp_id], [dv_id], [em_number], [email], [emergency_relation], [option1], [status], [em_id], [em_title], [date_hired])  

 ON [Target].[em_id] = [Source].[em_id]

 WHEN MATCHED THEN
     UPDATE SET [dp_id]             = [Source].[dp_id],
                [dv_id]                 = [Source].[dv_id],
                [em_number]             = [Source].[em_number],
                [email]                 = [Source].[email],
                [emergency_relation]    = [Source].[email],
                [option1]               = [Source].[option1],
                [status]                = [Source].[status],
                [em_id]                 = [Source].[em_id],
                [em_title]              = [Source].[em_title],
                [date_hired]            = [Source].[date_hired],

WHEN NOT MATCHED THEN

    INSERT ([dp_id], [dv_id], [em_number], [email], [emergency_relation], [option1], [status], [em_id], [em_title],[date_hired])
    VALUES ([Source].[dp_id], [Source].[dv_id], [Source].[em_number], [Source].[email], [Source].[emergency_relation], [Source].[option1], [Source].[status], [Source].[em_id], [Source].[em_title], [Source].[date_hired]);
GO

我在第WHEN NOT MATCHED THEN

上收到错误
  

关键字' WHEN'附近的语法不正确。

我是存储过程的新手,并试图找出它但不能。

1 个答案:

答案 0 :(得分:4)

你不想要这个逗号 -

UPDATE SET [dp_id]             = [Source].[dp_id],
            [dv_id]                 = [Source].[dv_id],
            [em_number]             = [Source].[em_number],
            [email]                 = [Source].[email],
            [emergency_relation]    = [Source].[email],
            [option1]               = [Source].[option1],
            [status]                = [Source].[status],
            [em_id]                 = [Source].[em_id],
            [em_title]              = [Source].[em_title],
            [date_hired]            = [Source].[date_hired]**,**