The IT Dejan
The IT Dejan

Reputation: 866

Check whether value of column exists across Mutiple tables

Problem:
I have several tables which have column called "Name". What I'm trying to do is to check whether any of those tables have value "John" in their "Name" columns.

I'm working on a WPF application, and I'm using Entity Framework (v6.1.3). I assume I cannot use EF for this specific problem, because I'm using Repository pattern, and one repository only works with one table.

If this is not solvable with EF, plain SQL is also okay so that I can finally understand how to do this.

SQL is not my best area of expertise, so thanks up front!

Upvotes: 0

Views: 48

Answers (3)

Niraj Rajpurohit
Niraj Rajpurohit

Reputation: 34

Try This :

Check Single Same Record :

select * FROM tab1 as t1 with(nolock) where t1.Name in (SELECT t2.Name FROM tab2 as t2 with(nolock) where t2.Name='John')

Check Multiple Same Record :

 select * FROM tab1 as t1 with(nolock) where t1.Name in (SELECT t2.Name FROM tab2 as t2 with(nolock))

Upvotes: 0

mm8
mm8

Reputation: 169360

You could iterate through the entities of each DbSet and check whether the Name property of any entity equals to "John".

Upvotes: 1

Lukasz Szozda
Lukasz Szozda

Reputation: 176024

You could use UNION ALL:

SELECT *
FROM (SELECT Name FROM tab1
      UNION ALL
      SELECT Name FROM tab2
      --...
) AS sub
WHERE sub.Name = 'John'

Upvotes: 1

Related Questions