Query Expressions and Uniform Resource Names
Topic Status: Some information in this topic is preview and subject to change in future releases. Preview information describes new features or changes to existing features in Microsoft SQL Server 2016 Community Technology Preview 2 (CTP2).
The SQL Server Management Object (SMO) models and SQL Server PowerShell snap-ins use two types of expression strings that are similar to XPath expressions. Query expressions are strings that specify a set of criteria used to enumerate one or more objects in an object model hierarchy. A Uniform Resource Name (URN) is a specific type of query expression string that uniquely identifies a single object.
Object1[<FilterExpression1>]/ ... /ObjectN[<FilterExpressionN>] <FilterExpression>::= <PropertyExpression> [and <PropertyExpression>][...n] <PropertyExpression>::= @BooleanPropertyName=true() | @BooleanPropertyName=false() | contains(@StringPropertyName, 'PatternString') | @StringPropertyName='String' | @DatePropertyName=datetime('DateString') | is_null(@PropertyName) | not(<PropertyExpression>)
Query expressions are strings that enumerate the nodes in an SMO model hierarchy. Each node has a filter expression that specifies the criteria for determining which objects at that node are enumerated. Query expressions are modeled on the XPath expression language. Query expressions implement a small subset of the expressions that are supported by XPath, and also have some extensions that are not found in XPath. XPath expressions are strings that specify a set of criteria that are used to enumerate one or more of the tags in an XML document. For more information about XPath, see W3C XPath Language.
Query expressions must start with an absolute reference to the Server object. Relative expressions with a leading / are not allowed. The sequence of objects that are specified in a query expression must follow the hierarchy of collection objects in the associated object model. For example, a query expression that references objects in the Microsoft.SqlServer.Management.Smo namespace must start with a Server node followed by a Database node, and so on.
If a <FilterExpression> is not specified for an object, all the objects at that node are enumerated.
Uniform Resource Names (URN)
URNs are a subset of query expressions. Each URN forms a fully-qualified reference to a single object. A typical URN uses the Name property to identify a single object at each node. For example, this URN refers to a specific column:
Server[@Name='MYCOMPUTER']/Database[@Name='AdventureWorks2012']/Table[@Name='SalesPerson' and @Schema='Sales']/Column[@Name='SalesPersonID']
A. Enumerating objects using false()
This query expression enumerates all the databases that have the AutoClose attribute set to false in the default instance on MyComputer.
B. Enumerating objects using contains
This query expression enumerates all the databases that are case-insensitive and have the character 'm' in their name.
Server[@Name='MYCOMPUTER']/Database[@CaseSensitive=false() and contains(@Name, 'm')]
C. Enumerating objects using not
This query expression enumerates all of AdventureWorks2012 tables that are not in the Production schema and contain the word History in the table name:
Server[@Name='MYCOMPUTER']/Database[@Name='AdventureWorks2012']/Table[not(@Schema='Production') and contains(@Name, 'History')]
D. Not supplying a filter expression for the final node
This query expression enumerates all the columns in the AdventureWorks2012.Sales.SalesPerson table:
Server[@Name='MYCOMPUTER']/Database[@Name='AdventureWorks2012"]/Table[@Schema='Sales' and @Name='SalesPerson']/Columns
E. Enumerating objects using datetime
This query expression enumerates all the tables that are created in the AdventureWorks2012 database at a specific time:
F. Enumerating objects using is_null
This query expression enumerates all the tables in the AdventureWorks2012 database that do not have NULL for their date last modified property: