向存储过程添加 IF ...ELSE 语句以跳过重复的主键

Posted

技术标签:

【中文标题】向存储过程添加 IF ...ELSE 语句以跳过重复的主键【英文标题】:Add a IF ...ELSE statement to stored procedure to skip duplicate primary keys 【发布时间】:2012-05-09 09:10:42 【问题描述】:

我在我的物理 C# 代码中有一个 try catch,它只是在出现错误时跳过此插入过程并继续循环以填充数据库。然而,这是一种糟糕的编码习惯。

所以我想在下面的存储过程中添加一个IF 语句,如果主键已经存在,它将跳过。我的主键是@id

我该怎么办?

CREATE PROCEDURE InsertProc
    (
    @id int, 
    @to nvarchar(100), 
    @from nvarchar(100), 
    @subject nvarchar(100), 
    @date datetime
    )
AS
    INSERT INTO Emails_Log (Email_ID, e_To, e_From, e_Subject, e_Date) 
    VALUES (@id, @to, @from, @subject, @date)

【问题讨论】:

【参考方案1】:
CREATE PROCEDURE InsertProc
    (
    @id int, 
    @to nvarchar(100), 
    @from nvarchar(100), 
    @subject nvarchar(100), 
    @date datetime
    )
    AS
    IF NOT EXISTS (SELECT NULL FROM Emails_Log
                    WHERE Email_ID = @ID)
    BEGIN
        INSERT INTO Emails_Log (Email_ID, e_To, e_From, e_Subject, e_Date) 
            VALUES (@id, @to, @from, @subject, @date)
    END

如果你真的想更新记录,如果已经存在,如果不存在则插入,模式如下:

CREATE PROCEDURE InsertProc
    (
       @id int, 
       @to nvarchar(100), 
       @from nvarchar(100), 
       @subject nvarchar(100), 
       @date datetime
    )
    AS
       UPDATE Emails_Log
          SET e_To = @to, 
              e_From = @from, 
              e_Subject = @subject, 
              e_Date = @date
        WHERE Email_ID = @ID
       -- If there was no update it means that @ID does not exist,
       -- So we proceede with insert
       IF @@ROWCOUNT = 0
       BEGIN
          INSERT INTO Emails_Log (Email_ID, e_To, e_From, e_Subject, e_Date) 
               VALUES (@id, @to, @from, @subject, @date)
       END

【讨论】:

【参考方案2】:
CREATE PROCEDURE InsertProc
    (
    @id int, 
    @to nvarchar(100), 
    @from nvarchar(100), 
    @subject nvarchar(100), 
    @date datetime
    )
    AS
    IF EXISTS(SELECT * From Emails_Log Where Email_ID = @id)
    UPDATE Emails_Log (Email_ID, e_To, e_From, e_Subject, e_Date) 
            SET e_To = @to, e_From = @from, e_Subject = @subject, e_Date = @date
            WHERE Email_ID = @id
    ELSE
    INSERT INTO Emails_Log (Email_ID, e_To, e_From, e_Subject, e_Date) 
            VALUES (@id, @to, @from, @subject, @date)

另一种方法是使用MERGE 命令。

看this thread on SO to know more。

【讨论】:

设置...? ((at)id, (at)to, (at)from, (at)subject, (at)date) 会进去吗? Msg 102,级别 15,状态 31,过程 InsertUpdateProc,第 11 行 '(' 附近的语法错误。:我收到此错误【参考方案3】:

您可以检查表中是否存在记录并 在一条语句中插入记录。

【讨论】:

【参考方案4】:

试试这个代码

CREATE PROCEDURE InsertProc
(
@id int, 
@to nvarchar(100), 
@from nvarchar(100), 
@subject nvarchar(100), 
@date datetime
)
AS
IF NOT EXISTS (SELECT Email_ID From Emails_Log Where Email_ID = @id)
BEGIN
    INSERT INTO Emails_Log (Email_ID, e_To, e_From, e_Subject, e_Date) 
        VALUES (@id, @to, @from, @subject, @date)
END

【讨论】:

以上是关于向存储过程添加 IF ...ELSE 语句以跳过重复的主键的主要内容,如果未能解决你的问题,请参考以下文章

Oracle 存储过程 在循环裏面,怎麽写2个IF语句?不是IF...ELSE IF 哦 因为2个代码块都要判断

oracle存储过程技术怎么就那么不规范?if else if 再多个else if就不能用了?

MySQL存储过程语法错误:if else

oracle 存储过程里的if else

添加另一个方法后,dispatch()和if-else语句出现问题

在mysql中用if…else语句判断当天是不是为星期天?