如何插入到表中但仅当记录不存在时

时间:2012-06-28 12:16:20

标签: sql-server sql-server-2005

我有一张存储以下内容的表格:

JobID
ValidationItemID
CreatedBy

我希望能够插入此表(预定义的模板),但只添加不存在的行。我的意思是不存在是JobID和ValidationItemID的组合使行唯一。我的程序传入一个JobID,但我无法传递验证项ID,因为我将此列作为模板的一部分...

有这样的效果:

CREATE PROCEDURE insTemplate 
    @JobID varchar(50),
    @Login varchar(50)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    INSERT INTO
            ValidationItemSignOff
                (
                    JobID,  
                    ValidationItemID,
                    CreatedBy
                )

        SELECT 
        DISTINCT
            @JobID,
            vi.ValidationItemID,
            @Login
        FROM
            RunOffAnswer roa
        INNER JOIN
            Method m ON m.MethodID = roa.MethodID
        INNER JOIN
            RunOffValidationItem vi ON vi.ValidationItemID = m.ValidationItemID
        WHERE
            vi.Inactive=0
            AND NOT EXISTS(SELECT * FROM ValidationItemSignOff WHERE JobID=@JobID AND vi.ValidationItemID ???
END
GO

我不知道如何短语where条件,以便它不会重新插入相同的JobID和ValidationItemID。让我们说我在桌子里面:

Job      ValidationItem
Job A          1
Job A          2
Job A          5

我有一个包含以下内容的模板:

ValidationItem
1
2
3
4
5
6

当我运行我的存储过程时,它应该只从模板表插入值3,4,6,作为作业ID ...所以我需要帮助我的where条件。

我认为我的问题是我无法使用NOT EXISTS,也许我需要在JobIDValidationItemID上加入这个ValidationItemSignOff表,其中ValidationItemID为NULL,也许像这样:

CREATE PROCEDURE insTemplate 
    @JobID varchar(50),
    @Login varchar(50)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    INSERT INTO
            ValidationItemSignOff
                (
                    JobID,  
                    ValidationItemID,
                    CreatedBy
                )

        SELECT 
        DISTINCT
            @JobID,
            vi.ValidationItemID,
            @Login
        FROM
            RunOffAnswer roa
        INNER JOIN
            Method m ON m.MethodID = roa.MethodID
        INNER JOIN
            RunOffValidationItem vi ON vi.ValidationItemID = m.ValidationItemID
        RIGHT OUTER JOIN
            ValidationItemSignOff viso ON viso.JobID = @JobID
                  AND viso.ValidationItemID = vi.ValidationItemID
        WHERE
            vi.Inactive=0
            AND viso.ValidationItemID IS NULL
END
GO

我认为

用此替换右连接:

LEFT JOIN
        ValidationItemSignOff viso
        ON  viso.JobID = @JobID
        AND viso.ValidationItemID = vi.ValidationItemID

2 个答案:

答案 0 :(得分:1)

如果您是2008年或以上......

MERGE INTO ValidationItemSignOff As Target
USING (SELECT DISTINCT @JobID, vi.ValidationItemID, @Login
         FROM RunOffAnswer roa INNER JOIN 
              Method m
           ON m.MethodID = roa.MethodID INNER JOIN
              RunOffValidationItem vi
           ON vi.ValidationItemID = m.ValidationItemID
        WHERE vi.Inactive = 0) As Source (JobID, ValidationItemID, Login)
   ON Target.JobID = Source.JobID 
  AND Target.ValidationItemID = Source.ValidationItemID
 WHEN NOT MATCHED BY TARGET THEN
      INSERT (JobID, ValidationItemID, CreatedBy)
      VALUES (Source.JobID, Source.ValidationItemID, Source.Login);

免责声明:我可能在这里没有语法点。

答案 1 :(得分:0)

我得到了它:

ALTER PROCEDURE insSignOffTemplate 
    @JobID varchar(50),
    @Login varchar(50)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    INSERT INTO
            ValidationItemSignOff
                (
                    JobID,  
                    ValidationItemID,
                    CreatedBy
                )

        SELECT 
        DISTINCT
            @JobID,
            vi.ValidationItemID,
            @Login
        FROM
            RunOffAnswer roa
        INNER JOIN
            Method m
        ON
            m.MethodID = roa.MethodID
        INNER JOIN
            RunOffValidationItem vi
        ON
            vi.ValidationItemID = m.ValidationItemID
        LEFT JOIN
        ValidationItemSignOff viso
        ON  viso.JobID = @JobID     AND viso.ValidationItemID = vi.ValidationItemID
        WHERE
            vi.Inactive=0
            AND viso.ValidationItemID IS NULL
END
GO