Click to See Complete Forum and Search --> : Question on subtracting two queries


xmlprogrammer
10-04-2007, 12:05 PM
I want to return the difference number of records.

Here are the queries:


--subtracting columns with columns and descriptions (columns - columns and descriptions)
--difference of 30 records
--243
select
h.name as 'DataBase Name'
,t.name as 'Table Name'
,c.name as 'Column Name'
from sys.tables t
inner join sys.schemas h
on h.schema_id = t.schema_id
inner join sys.columns c
on t.object_id = c.object_id
and not exists
(
--213
select
h.name as 'DataBase Name'
,t.name as 'Table Name'
,c.name as 'Column Name'
from sys.extended_properties s
inner join sys.tables t
on s.major_id = t.object_id
inner join sys.schemas h
on h.schema_id = t.schema_id
inner join sys.columns c
on s.major_id = c.object_id
and s.minor_id = c.column_id
)


I'm not getting anything back. I should be getting back 30 records that have null descriptions.

Please help.

bubbisthedog
10-04-2007, 02:35 PM
Returning no rows is the expected result. EXISTS only requires the subquery to return at least one result; if so, then it evaluates to 'true.' Here's what you're literally asking:

Show me all results from my main query if no records are returned from my subquery.

Thus you'll get either 0 or 243 records returned. Because at least one record is returned from your subquery, 0 records from your main query are returned.

EXISTS is not what you want to be using. I'd research the IN keyword maybe. I'm not sure what 'disqualifies' a record from being returned from your main query. Are you wanting to return a record from the main query if 'DataBase Name', 'Table Name', 'Column Name' does not exist in a subquery record?

xmlprogrammer
10-04-2007, 02:46 PM
Thank you bubbisthedog.

The answer to your question is no. What I'm actually trying to do is retrieve all tables that do not have a description assigned to it.

The way I was thinking was if I subtract the total number of columns that have descriptions from the total number of all columns, I would get back only the columns that do not have any descriptions. In otherwords, it would look something like this:

column_name description

column1 NULL

Note: the alignment in the above example may be off. Bold goes with the letter right below that is not bold.

xmlprogrammer
10-05-2007, 07:22 AM
select
h.name as 'DataBase Name'
,t.name as 'Table Name'
,c.name as 'Column Name'
from sys.tables t
inner join sys.schemas h
on h.schema_id = t.schema_id
inner join sys.columns c
on t.object_id = c.object_id
left join sys.extended_properties s
on s.major_id = c.object_id
and s.minor_id = c.column_id
where s.minor_id is null