user2165345
user2165345

Reputation: 67

Combining NOT EXISTS with INSERT ... SELECT

The following code inserts a new row into a table based on a value in another row but it also allows duplicate data. I want to use NOT EXISTS so that if there is already a row with that value then it won't insert another one, but not sure how to integrate this.

INSERT INTO [Grading].[dbo].[tblObservations]
       ([FormID]
       ,[Data]
       ,[UserID]
       ,[DateOfObservation]
       ,[Final]
       ,[ValidTo]
       ,[ID_Grading]
       ,[ID_ObservationKind]
       ,[Created]
       ,[Modified]
       ,[RowVersion])
SELECT [FormID]
  ,'0'
  ,[UserID]
  ,[DateOfObservation]
  ,[Final]
  ,[ValidTo]
  ,[ID_Grading]
  ,40
  ,[Created]
  ,[Modified]
  ,[RowVersion]
  FROM [Grading].[dbo].[tblObservations]
  WHERE [ID_ObservationKind] = 39 AND [Data] = 'No' AND [Final] = 1

Any help appreciated.

Upvotes: 1

Views: 100

Answers (3)

Devart
Devart

Reputation: 121902

Try this one -

USE [Grading]

INSERT INTO [dbo].[tblObservations]
(
      FormID
    , Data
    , UserID
    , DateOfObservation
    , Final
    , ValidTo
    , ID_Grading
    , ID_ObservationKind
    , Created
    , Modified
    , [RowVersion]
)
SELECT 
      t.FormID
    , '0'
    , t.UserID
    , t.DateOfObservation
    , t.Final
    , t.ValidTo
    , t.ID_Grading
    , 40
    , t.Created
    , t.Modified
    , t.[RowVersion]
FROM dbo.tblObservations t
LEFT JOIN dbo.tblObservations t2 ON 
        t.FormID = t2.FormID
    AND t2.Data = '0'
    AND t.UserID = t2.UserID
    AND t.DateOfObservation = t2.DateOfObservation
    AND t.Final = t2.Final
    AND t.ValidTo = t2.ValidTo
    AND t.ID_Grading = t2.ID_Grading
    AND t2.ID_ObservationKind = 40
    AND t.Created = t2.Created
    AND t.Modified = t2.Modified
    AND t.[RowVersion] = t2.[RowVersion]
WHERE t.ID_ObservationKind = 39 
    AND t.Data = 'No' 
    AND t.Final = 1
    AND t2.FormID IS NULL

Or try this -

INSERT INTO [dbo].[tblObservations]
(
      FormID
    , Data
    , UserID
    , DateOfObservation
    , Final
    , ValidTo
    , ID_Grading
    , ID_ObservationKind
    , Created
    , Modified
    , [RowVersion]
)
SELECT 
      t.FormID
    , '0'
    , t.UserID
    , t.DateOfObservation
    , t.Final
    , t.ValidTo
    , t.ID_Grading
    , 40
    , t.Created
    , t.Modified
    , t.[RowVersion]
FROM dbo.tblObservations t
WHERE t.ID_ObservationKind = 39 
    AND t.Data = 'No' 
    AND t.Final = 1
    AND NOT EXISTS(
        SELECT 1 
        FROM dbo.tblObservations t2
        WHERE t2.ID_ObservationKind = 39 
            AND t2.Data = 'No' 
            AND t2.Final = 1
    )

Upvotes: 1

Tim Dorr
Tim Dorr

Reputation: 4921

You're looking to do a "merge" or "upsert" in SQL. Depending on your database, there are a number of ways to do this. If you are using MySQL, you'll want to use it's INSERT... ON DUPLICATE KEY UPDATE syntax.

Upvotes: 0

Satpal
Satpal

Reputation: 133403

You can try

IF NOT EXISTS
    (
    SELECT 1
FROM [Grading].[dbo].[tblObservations]
WHERE [ID_ObservationKind] = 39 AND [Data] = 'No' AND [Final] = 1
    )

    BEGIN
        -- Insert script
    END

Upvotes: 1

Related Questions