存储过程以更新或插入

时间:2015-04-06 14:55:33

标签: sql-server stored-procedures biztalk

我是存储过程的新手,并设法google,创建用于插入或更新数据库的存储过程。从Oracle数据库中选择记录集,并根据em_id,使用BizTalk将其记录或更新到Sql Database表中 尝试创建存储过程以根据字段插入或更新记录。

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].[emergency_relation],
            [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'附近的语法不正确。

有评论显示代码容易出现死锁。由于我是Stored Procedures的新手,我不知道如何创建存储过程以便插入或更新而不会出现死锁。我真的被卡住了。

1 个答案:

答案 0 :(得分:0)

合并是死锁的原因,你可以简单地试试

        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

     IF ( (SELECT COUNT(em_id) FROM [dbo].[em] WHERE em_id = @em_id) > 0)
      UPDATE [dbo].[em] 
      SET 
                [dp_id]                 = @dp_id,
                [dv_id]                 = @dv_id,
                [em_number]             = @em_number,
                [email]                 = @email,
                [emergency_relation]    = @emergency_relation,
                [option1]               = @option1,
                [status]                = @status,
                [em_id]                 = @em_id,
                [em_title]              = @em_title,
                [date_hired]            = @date_hired
WHERE em_id = @em_id

    ELSE

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