Arianule
Arianule

Reputation: 9043

Checking that a value from a table does not exists in another table sql

I have a Table Valued Parameter that contains guids as a field and I want to enter it's values into another table where the guid does not exist

Which is the best way to do this.

The check I am doing at the moment doesnt seem right.

IF NOT EXISTS(SELECT F.FieldKey FROM @Fields F INNER JOIN DomainObjectFields DOF ON F.FieldKey = DOF.UniqueKey)

--@Fields is the tvp and DomainObjectFields is the regular table I want to add values to

regards

Upvotes: 0

Views: 113

Answers (1)

Steve Pettifer
Steve Pettifer

Reputation: 2043

I think I'd use a LEFT OUTER JOIN myself.

INSERT INTO DomainObjectFields (UniqueKey)
SELECT F.FieldKey 
FROM @Fields F 
LEFT OUTER JOIN DomainObjectFields DOF 
    ON DOF.FieldKey = F.UniqueKey
WHERE DOF.UniqueKey IS NULL

Upvotes: 3

Related Questions