Mindaugas
Mindaugas

Reputation: 163

How to check if SQL Server table has rows and return table name as column name and value true or false

I need to create a view which will query a few specified tables and return a view table with one row. Table must be formed with column with table names and one row must be returned with values True/False depending on the table have rows or not.

For example, I have four tables:

Medication / Patient / User / Appointment

I need to check them tables if they have rows or not and return a view table with one row and columns like that.

Medication | Patient | User | Appointment
True       | False   | False| True

Upvotes: 0

Views: 3956

Answers (1)

Blachshma
Blachshma

Reputation: 17395

There are quite a few ways to do this, if it is just these 4 tables you can use this:

select
(select Case When (select COUNT(*) FROM Medication) > 0 then 'true' else 'false' end) as Medication,
(select Case When (select COUNT(*) FROM Patient) > 0 then 'true' else 'false' end) as Patient,
(select Case When (select COUNT(*) FROM Appointment) > 0 then 'true' else 'false' end) as Appointment

Upvotes: 1

Related Questions