带有可选列的 WQL SELECT
我需要进行如下查询:
SELECT PNPDeviceID FROM Win32_NetworkAdapter WHERE AdapterTypeId = 0
问题是,AdapterTypeId 列并不总是存在。在这种情况下,我只想要一切,就像这样:
SELECT PNPDeviceID FROM Win32_NetworkAdapter
我的 WQL/SQL 知识极其有限。谁能告诉我如何在单个查询中执行此操作?
编辑:
似乎需要更多背景知识:我正在使用 WMI 查询 Windows 设备信息,它使用类似 SQL 的语法。因此,在我的示例中,我正在查询 AdapterTypeId 为 0 的网络适配器。
然而,该列并不总是存在,这意味着如果我枚举返回的值,则不会列出“AdapterTypeId”。
编辑2:
将SQL改为WQL;显然这是更正确的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我假设您的意思是底层架构不可靠。
这是一种非常非常规的情况。我建议您解决导致该列并不总是存在的问题,因为在应用程序下动态更改架构可能(几乎肯定)是灾难性的。
更新:
好的,WQL 允许您使用类似 SQL 的语法查询对象,但与 SQL 不同的是,架构可以在您的脚下更改。这是一个泄漏抽象的经典示例,我现在讨厌 WQL,但从未使用过它:)。
由于可用属性不断变化,我猜测 WQL 提供了一种枚举给定适配器属性的方法。执行此操作,并根据结果选择要运行的查询。
经过一番谷歌搜索后,有一个示例 此处,显示如何枚举可用属性。您可以使用它来确定
AdapterTypeId
是否存在。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.我假设您的意思是表中缺少该字段。
您在提交查询之前知道该字段是否存在吗?
如果是,则动态创建 SQL,否则它认为在缺少字段的情况下会出现语法错误
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
这不是一个 SQL 问题。 SQL 不考虑在单个表源中使用不同模式的记录。相反(正如您所提到的)这是一个使用“类似 SQL”语法的不同系统。如果您使用您尝试查询的实际产品重新提出问题,您将会有更好的运气,并且文档中可能讨论了该产品如何处理可变记录结构的信息。
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.