Query Expressions and Unique Resource Names
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 Unique Resource Name (URN) is a specific type of query expression string that uniquely identifies a single object.
Object1[<FilterExpression1>]/ ... /ObjecN[<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.
Unique 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='AdventureWorks']/Table[@Name='SalesPerson' and @Schema='Sales']/Column[@Name='SalesPersonID']
Server[@Name='MYCOMPUTER']/Database[@Name='AdventureWorks']/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.
Server[@Name='MYCOMPUTER']/Database[@AutoClose=false()]
Server[@Name='MYCOMPUTER']/Database[@AutoClose=false()]
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')]
Server[@Name='MYCOMPUTER']/Database[@CaseSensitive=false() and contains(@Name, 'm')]
C. Enumerating objects using not
This query expression enumerates all of AdventureWorks tables that are not in the Production schema and contain the word History in the table name:
Server[@Name='MYCOMPUTER']/Database[@Name='AdventureWorks']/Table[not(@Schema='Production') and contains(@Name, 'History')]
Server[@Name='MYCOMPUTER']/Database[@Name='AdventureWorks']/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 AdventureWorks.Sales.SalesPerson table:
Server[@Name='MYCOMPUTER']/Database[@Name='AdventureWorks"]/Table[@Schema='Sales' and @Name='SalesPerson']/Columns
Server[@Name='MYCOMPUTER']/Database[@Name='AdventureWorks"]/Table[@Schema='Sales' and @Name='SalesPerson']/Columns
E. Enumerating objects using datetime
This query expression enumerates all the tables that are created in the AdventureWorks database at a specific time:
Server[@Name='MYCOMPUTER']/Database[@Name='AdventureWorks"]/Table[@CreateDate=datetime('2008-03-21 19:49:32.647')]
Server[@Name='MYCOMPUTER']/Database[@Name='AdventureWorks"]/Table[@CreateDate=datetime('2008-03-21 19:49:32.647')]
F. Enumerating objects using is_null
This query expression enumerates all the tables in the AdventureWorks database that do not have NULL for their date last modified property:
Server[@Name='MYCOMPUTER']/Database[@Name='AdventureWorks"]/Table[Not(is_null(@DateLastModified))]
Server[@Name='MYCOMPUTER']/Database[@Name='AdventureWorks"]/Table[Not(is_null(@DateLastModified))]
