Skip to content
Convert WQL Queries to SQL Queries using SCCM Trick

Convert WQL Queries to SQL Queries using SCCM Trick

Written By Anoop C Nair
Last Updated September 30, 2021
Posted In SCCM
SHARE

Let’s see how to convert WQL queries to SQL queries using SCCM trick. This is the least expected functionality from Configuration Manager as a device management tool. The good news is that SCCM can help you to convert WQL to SQL queries.

I don’t know whether there is any tool available in the market to convert queries from WQL to SQL. This is not something SCCM will support in the console as a feature. But, you can use the SCCM method/trick that explained below to convert queries from WQL to SQL.

There is no other easy method to convert the WQL queries used in dynamic collections into SQL queries. I have seen questions in the HTMD forum asking for SQL queries when you answer with WQL queries. Hopefully, the method explained in this post will help you to convert all the WQL queries used in collections to SQL queries.

NOTE! – This post is only for educational purposes. Make sure you are using the supported method to build SQL queries. I don’t know whether this is a 100% supported method from Microsoft’s perspective.

Patch My PC

WQL Query to group Co-Managed Devices

I have a WQL query to find out co-managed devices, and I wanted to convert the SQL queries using the method or trick that is not fully supported as per Microsoft. When I say not fully supported, that means Microsoft never tested and approved it use.

You can try to find out the SQL views for each WMI class similar to SMS_Client_ComanagementState and SMS_R_System. But this is not very easy but this is not impossible. The following list provides the mapping between WMI namespaces and SQL views. These mapping details can also be retrieved using the SCCM tricks explained below.

  • WQL – SMS_Client_ComanagementState – > SQL v_ClientCoManagementState
  • WQL – SMS_R_System -> SQL – v_R_System
Convert WQL Queries to SQL Queries using SCCM Trick
WQL Query to group Co-Managed Devices – Convert WQL Queries to SQL Queries using SCCM Trick

You can use the following WQL query to create a dynamic device collection. The dynamic device collection creation process will be followed in the below section to convert WQL queries to SQL queries.

select SMS_R_SYSTEM.ResourceID,SMS_R_SYSTEM.ResourceType,SMS_R_SYSTEM.Name,SMS_R_SYSTEM.SMSUniqueIdentifier,SMS_R_SYSTEM.ResourceDomainORWorkgroup,SMS_R_SYSTEM.Client from SMS_R_System  inner join SMS_Client_ComanagementState on SMS_Client_ComanagementState.ResourceId = SMS_R_System.ResourceId   where SMS_Client_ComanagementState.ComgmtPolicyPresent = 1 AND SMS_Client_ComanagementState.MDMEnrolled = 1 AND MDMProvisioned = 1

SQL Query to Find Co-Management Devices

Now, let’s find the SQL query that you can get from this conversion method. I don’t think the SQL query that you get from the conversion process is not similar to the standard SQL queries. However, you can use this method to get an idea about the conversion process and get an idea about the SQL tables in use.

There are many other SQL query-related posts that you can find from the HTMD blog posts mentioned below. I think it’s better to use SQL views to create custom reports using SQL queries.

 SQL Query to Find Co-Management Devices
SQL Query to Find Co-Management Devices

It’s interesting to see the conversion trick from WQL queries to SQL queries that use SQL views instead of SQL Tables. It’s worth noting that using SQL tables to create a custom report is not the best practice. Instead, it’s better to use SQL views.

select  all SMS_R_SYSTEM.ItemKey,SMS_R_SYSTEM.DiscArchKey,SMS_R_SYSTEM.Name0,SMS_R_SYSTEM.SMS_Unique_Identifier0,
 SMS_R_SYSTEM.Resource_Domain_OR_Workgr0,SMS_R_SYSTEM.Client0 from vSMS_R_System AS SMS_R_System 
 INNER JOIN v_ClientCoManagementState AS SMS_Client_ComanagementState 
 ON SMS_Client_ComanagementState.ResourceID = SMS_R_System.ItemKey   
 where ((SMS_Client_ComanagementState.ComgmtPolicyPresent = 1 
 AND SMS_Client_ComanagementState.MDMEnrolled = 1) AND SMS_Client_ComanagementState.MDMProvisioned = 1)

Convert WQL Queries to SQL Queries using SCCM Trick

Well the trick to convert WQL queries to SQL is not rocket science. You will need to create a dynamic device collection with the WQL query that you want to convert. Building SQL queries and reports explained in the previous post – SCCM Create Custom Report Using Report Builder.

You will need to go through the process of dynamic collection creation as explained in How-to Guide Create ConfigMgr Dynamic Device Collection. You can create either a dynamic user collection or a dynamic device collection to convert the queries from WQL to SQL.

Make sure you open SMS Provider log file (SMSProv.log) from the site server or SMS provide site system server to monitor the changes before completing the dynamic collection creation wizard. Open the SCCM console and SMSProv.log side by side to monitor the changes at the same time.

Convert WQL Queries to SQL Queries using SCCM Trick
Convert WQL Queries to SQL Queries using SCCM Trick

Once you finish the dynamic collection creation process, you can see the SQL query that is equivalent to the WQL query used in the dynamic collection creation process. It’s very useful that the log file even spits out the respective SCCM SQL views of those WMI classes used in the WQL query.

WQL Query from SMSProv.log -> Amended CR query string = select SMS_R_SYSTEM.ResourceID,SMS_R_SYSTEM.ResourceType,SMS_R_SYSTEM.Name,SMS_R_SYSTEM.SMSUniqueIdentifier,SMS_R_SYSTEM.ResourceDomainORWorkgroup,SMS_R_SYSTEM.Client from SMS_R_System  inner join SMS_Client_ComanagementState on SMS_Client_ComanagementState.ResourceId = SMS_R_System.ResourceId   where SMS_Client_ComanagementState.ComgmtPolicyPresent = 1 AND SMS_Client_ComanagementState.MDMEnrolled = 1 AND MDMProvisioned = 1
SQL Query for the above WQL Query -> Literal SQL string = select  all SMS_R_SYSTEM.ItemKey,SMS_R_SYSTEM.DiscArchKey,SMS_R_SYSTEM.Name0,SMS_R_SYSTEM.SMS_Unique_Identifier0,SMS_R_SYSTEM.Resource_Domain_OR_Workgr0,SMS_R_SYSTEM.Client0 from vSMS_R_System AS SMS_R_System INNER JOIN v_ClientCoManagementState AS SMS_Client_ComanagementState ON SMS_Client_ComanagementState.ResourceID = SMS_R_System.ItemKey   where ((SMS_Client_ComanagementState.ComgmtPolicyPresent = 1 AND SMS_Client_ComanagementState.MDMEnrolled = 1) AND SMS_Client_ComanagementState.MDMProvisioned = 1)

The useful piece is all about spitting out corresponding SQL views. You can check the following two lines of details from the SMSProv.log to get the details of SQL views. It’s SQL views even though log calls this out as SQL tables.

  • Referenced SQL table = vSMS_R_System
  • Referenced SQL table = v_ClientCoManagementState

Author

Anoop is Microsoft MVP! He is a Solution Architect in enterprise client management with over 17 years of experience (calculation done in 2018). He is Blogger, Speaker, and Local User Group HTMD Community leader. His main focus is on Device Management technologies like SCCM 2012, Current Branch, and Intune. He writes about ConfigMgr, Windows 11, Windows 10, Azure AD, Microsoft Intune, Windows 365, AVD, etc…..…

Written by

Anoop C Nair is Workplace Technology solution architect with 25+ years of experience in global enterprise organizations such as JP Morgan, Capgemini, etc. Microsoft Certified Trainer. Microsoft MVP from 2015 onwards for consecutive 11+ years! He also conducts Intune and modern workplace tech training for enterprise organizations. He is Blogger, Speaker, and Founder of HTMD Community and HTMD Conference. His main focus is on Device Management technologies like Intune, Windows, Cloud PC. He writes about technologies like Intune, SCCM, Windows, Cloud PC, Windows, Entra, Microsoft Security.

Discussion · 1 comment

Join the discussion

Your email address will not be published. Required fields are marked *

Related guides

Intune

Windows 11 KB5101650 KB5099414 July 2026 Patch and 3 Zero Day Vulnerabilities and 570 Flaws

Key Takeaways Windows 11 KB5101650 KB5099414 July 2026 Patch and 3 Zero Day Vulnerabilities and 570 Flaws! In the July 2026 Patch, Microsoft introduced new features designed to improve the overall Windows experience. The update adds enhancements to Windows Update for more flexible update management and introduces Point-in-Time Restore, providing an additional recovery option for […]

AC Anoop C Nair 9 min read
Intune

2026 June KB5094126 KB5093998 Windows 11 Patch | 3 Zero Day Vulnerabilities and 200 Flaws

Key Takeaways 2026 June KB5094126 KB5093998 Windows 11 Patch | 3 Zero Day Vulnerabilities and 200 Flaws! The June 2026 Windows 11 Patch Tuesday update brings several improvements to File Explorer. It adds support for additional archive formats, including UU, CPIO, XAR, and NuGet Packages (NUPKG). The update also preserves View and Sort preferences in […]

AC Anoop C Nair 10 min read
Intune

2026 May KB5089549 KB5087420 Windows 11 Patch | 0 Zero Day Vulnerabilities and 120 Flaws

Key Takeaways The Windows 11 May 2026 Patch KB5089549 KB5087420 Update brings important security fixes, performance improvements, and reliability enhancements across the operating system. The update introduces new features such as Xbox Mode for gaming, File Explorer improvements, enhanced input and sharing experiences, better taskbar and Windows Hello reliability, and additional enterprise management capabilities for […]

AC Anoop C Nair 8 min read
SCCM

ConfigMgr 2603 Introduces New Early Update Enrollment Process

Key Takeaways In this post we are discussing the ConfigMgr 2603 Introduces New Early Update Enrollment Process. Microsoft has officially released Configuration Manager version 2603 to the Early Update Ring, giving organizations an opportunity to test upcoming improvements before the global production rollout. The release is targeted at enterprises running ConfigMgr version 2409 or later […]

AC Anoop C Nair 3 min read