5

WQL (basically SQL for WMI) does not support a TOP or LIMIT keyword. Sql Server used TOP and many other RDBMSs supprt LIMIT etc.

Is there a workaround to emulating a SELECT query to behave as though it had a TOP/LIMIT clause that limits the result set to some arbitrary number?

Or is there some other WQL-specific keyword that works like TOP or LIMIT?

remi.chateauneu
  • 109
  • 2
  • 9
Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
  • How are you intending to use TOP? Most times it's a matter of using MAX/MIN value comparisons... – OMG Ponies Oct 15 '09 at 19:02
  • @rexem: Arbitrarily. For the sake of example, say there's a datetime column and you want to see the 10 most recent events. Is there a way to use MIN/MAX to emulate TOP? – Paul Sasik Oct 15 '09 at 19:16

3 Answers3

4

Nope, there's no way to simulate TOP using WQL alone.

Exception: if you're lucky enough to be querying a WMI class which has ungapped, ascending numeric instance numbers used as keys, then you can use greater-than and less-then comparisons to limit and page through the results.

It's possible that ManagementClass.GetInstances() instead of using a WQL query might allow you to cancel the enumeration midway once you've collected enough instances, and hence avoid paying the CPU and RAM cost of enumerating the whole list at once.

Note that, AFAIK, the CIMV2 WMI provider doesn't natively handle WQL-- instead it simply relies on WMI to enumerate all instances, process the WQL, and filter the results before returning them to the caller. But the expensive part (actually fetching the underlying WMI data) is still done. So I believe there's no efficiency gain to be had (for local WMI queries, that is) by using WQL vs. using GetInstances() and filtering the results yourself-- and if GetInstances() allows you to cancel midway, then GetInstances() may be much cheaper for long result sets.

remi.chateauneu
  • 109
  • 2
  • 9
Justin Grant
  • 44,807
  • 15
  • 124
  • 208
  • i found this: http://stackoverflow.com/questions/595123/is-there-an-ansi-sql-alternative-to-the-mysql-limit-keyword Any chance that WQL supports one of those "TOP" syntax alternatives from the answer? – Paul Sasik Oct 15 '09 at 20:10
  • Nope. WQL does not support this feature. – Justin Grant Oct 15 '09 at 20:22
1

Like Justin has said. I am not sure about your exact requirement though. I was doing a simple project with Visual Basic and part of it was to fetch event log, the listview fails due to the huge size of Application log(>20MB) and the control enters into some kind of infinite loop. I wanted to limit so here is the pseudocode

topval = UserInputText.Text 'Assuming user entered the top 10 or 20

while ThereAreStillItemsInCollection
 if topval = 0 then
 goto CleanUpMemObjectsAndReturn
 end if
topval = topval  - 1
wend
CleanUpMemObjectsAndReturn:
set obj = Nothing
end sub
Aliaksei Kliuchnikau
  • 13,589
  • 4
  • 59
  • 72
0

pipe it with "select xyz -First 1"

eg: Get-WmiObject win32_logicaldisk |select -First 1

user566047
  • 15
  • 1
  • 1
    This is the most useless comment here. Your WQL query still retrieves all of the objects and saves it in memory, but only the First 1 (in your example) gets displayed on the screen. This does not prevent all objects from being retrieved. In fact, if you had a WQL query that returned 1000 objects and it took 1 second per object, a `Select-Object -First 10` would output 1 object per second for the first 10 seconds and then the command would carry on for another 990 seconds with absolutely no feedback to the user whatsoever! – cogumel0 Oct 01 '14 at 09:48
  • Well, this answer was useful for my situation. Thanks! – afournier Mar 21 '18 at 17:00