tags:

views:

274

answers:

4

I need to make a query like this:

SELECT PNPDeviceID FROM Win32_NetworkAdapter WHERE AdapterTypeId = 0

Trouble is, the AdapterTypeId column isn't always present. In this case, I just want everything, like so:

SELECT PNPDeviceID FROM Win32_NetworkAdapter

My WQL/SQL knowledge is extremely limited. Can anybody tell me how to do this in a single query?

EDIT:
A bit more background seems to be required: I am querying Windows for device information using WMI, which uses an SQL-like syntax. So, in my example, I am querying for network adapters that have an AdapterTypeId of 0.
That column is not always present however, meaning that if I enumerate through the returned values then "AdapterTypeId" is not listed.

EDIT 2:
Changed SQL to WQL; apparantly this is more correct.

+2  A: 

I am assuming you mean the underlying schema is unreliable.

This is a highly unconventional situation. I suggest that you resolve the issue that is causing the column to not always be present, because to have the schema changing dynamically underneath your application is potentially (almost certainly) disastrous.

Update:

OK, so WQL lets you query objects with a SQL-like syntax but, unlike SQL, the schema can change underneath your feet. This is a classic example of a leaky abstraction, and I now hate WQL without ever having used it :).

Since the available properties are in flux, I am guessing that WQL provides a way to enumerate the properties for a given adapter. Do this, and choose which query to run depending upon the results.

After some Googling, there is an example here, which shows how to enumerate through the available properties. You can use this to determine if AdapterTypeId exists or not.

RedFilter
definitely. if it matters which fields are simply selected, change your code to account for it.
David
OK, so no-go on doing it in the query, then. Thanks for the effort!
NPVN
+1  A: 
SELECT PNPDeviceID FROM Win32_NetworkAdapter WHERE AdapterTypeId = {yourDesire} OR AdapterTypeId IS NULL
henchman
A: 

I assume that you mean that this field is missing from the table. Do you know before submitting the query if this field exists? If yes then just create SQL dynamically, otherwise It think you will get syntax error in case of missing field

Riho
A: 

This is not an SQL question. SQL does not contemplate records with varying schemas in a single table source. Instead (as you mention) this is a different system using an "SQL-like" syntax. You'll have better luck if you recast the question using the actual product that you're trying to query, and information how that product deals with variable record structures is probably discussed in the documentation.

Larry Lustig