Reputation: 5
I’m looking to create a program that checks if data from one row is in two different tables already, and fails if it finds them so it doesn’t create duplicate rows in a table.
Currently I have
If NOT EXISTS (SELECT 1 FROM table_a)
Which works to stop it from duplicating rows in table a, but I want it to not be able to insert a row if the data is also already in table b. What would be the best way to achieve this?
Upvotes: 0
Views: 230
Reputation: 12969
You can use NOT EXISTS with union all also.
IF NOT EXISTS(
SELECT 1 FROM Table_a WHERE condition
UNION ALL
SELECT 1 FROM Table_b WHERE condition
.
.
.
)
BEGIN
-- Your logic
END
Upvotes: 0
Reputation: 22811
You can check a number of tables with a condition kind of NOT EXISTS (SELECT 1 FROM table_a WHERE ..) AND NOT EXISTS (SELECT 1 FROM table_b WHERE ..)
Upvotes: 1