Groups | Blog | Home
all groups > sql server programming > march 2004 >

sql server programming : Conditional Select... (case, union, subselect?)



mwhitis NO[at]SPAM fuse.net
3/22/2004 10:16:22 PM
I'm trying to pull a report from my SMS database that will list a
yes/no type answer for each server, as to whether a file exists on
that system. There are basically 3 tables involved:

SoftwareInventory, which contains Columns (ClientID and FileID)
System_DATA, which joins to SoftwareInventory on ClientID and
SoftwareFile, which joins to SoftwareInventory on FileID.

Each system will have one row in System_Data which will join to many
in SoftwareInventory, which will link to one row in SoftwareFile, thus
making the coorelation.

If I just wanted a list of systems with the file, the syntax might
look like (I apolgize for any stylistic errors, as I'm not a guru):

SELECT System_Data.Name0,SoftwareFile.FileName
FROM System_Data
INNER JOIN SoftwareInventory on
System_Data.MachineID=SoftwareInventory.ClientID
INNER JOIN SoftwareFile on
SoftwareInventory.FileID-SoftwareFile.FileID
WHERE System_DATA.Name0 like '%SERVER% and SoftwareFile.FileName like
'foo.exe'

In order to get the yes/no type response, I though about using a CASE
in the select, like so:

SELECT System_DATA.Name0, CASE WHEN SoftwareFile.FileName like
'foo.exe' THEN 'YES' ElSE 'NO'

Which, and it suddenly seems obvious, returns a YES/NO for every file
on the system.

I guess what I'm looking for is for it to check SoftwareInventory for
the correct FileID that's associated with the particular machine ID.
If it finds it, then YES, else NO.

Is this something I should be using a subselect for? I'm not really
sure how to proceed with this.

Thanks,

mwhitis NO[at]SPAM fuse.net
3/23/2004 7:56:13 AM
Hugo,

Thanks for the reply. I've changed the like to = as you suggested. I
had gotten in the habit of using like because most of my queries are
wildcards.

These queries aren't quite what I'm looking for, though. When I run
them, they return a single YES or NO. Presumably, depdending on which
system it gets first from the System table. The output I need is list
of each system name, with it's status.. Like:

System_DATA.Name0|IsInstalled
MAILSERVER |YES
DATASERVER |YES
DATASERVER2 |NO

ETC..


Thanks,

Mike

[quoted text, click to view]
Hugo Kornelis
3/23/2004 9:46:27 AM
[quoted text, click to view]

First, you should replace "LIKE 'foo.exe'" with "= 'foo.exe'". Use
LIKE only for wildcard searches.

What you want can be achieved by:

IF EXISTS
(SELECT System_Data.Name0,SoftwareFile.FileName
FROM System_Data
INNER JOIN SoftwareInventory on
System_Data.MachineID=SoftwareInventory.ClientID
INNER JOIN SoftwareFile on
SoftwareInventory.FileID-SoftwareFile.FileID
WHERE System_DATA.Name0 like '%SERVER%
and SoftwareFile.FileName = 'foo.exe')
PRINT 'YES'
ELSE PRINT 'NO'

Or, if you must have it in a select statement:

SELECT CASE
WHEN EXISTS
(SELECT System_Data.Name0,SoftwareFile.FileName
FROM System_Data
INNER JOIN SoftwareInventory on
System_Data.MachineID=SoftwareInventory.ClientID
INNER JOIN SoftwareFile on
SoftwareInventory.FileID-SoftwareFile.FileID
WHERE System_DATA.Name0 like '%SERVER%
and SoftwareFile.FileName = 'foo.exe')
THEN 'YES'
ELSE 'NO'
END


Best, Hugo
--

Hugo Kornelis
3/23/2004 6:01:50 PM
[quoted text, click to view]

In that case, you need:

SELECT System_Data.Name0,
CASE
WHEN EXISTS
(SELECT *
FROM SoftwareInventory
INNER JOIN SoftwareFile on
SoftwareInventory.FileID = SoftwareFile.FileID
WHERE SoftwareInventory.ClientID = System_Data.MachineID
and SoftwareFile.FileName = 'foo.exe')
THEN 'YES'
ELSE 'NO'
END AS IsInstalled
FROM SystemData
WHERE System_Data.Name0 like '%SERVER%'

(untested)

Best, Hugo
--

mwhitis NO[at]SPAM fuse.net
3/24/2004 6:52:17 AM
Thanks Hugo. Once I looked at your code, I realized I was making it
FAR too complicated on myself. It's really just a simple subselect.
Once that clicked into place, I was a little embarassed that I didn't
see it before.

Thanks again

Mike

[quoted text, click to view]
AddThis Social Bookmark Button