{"id":270409,"date":"2015-12-15T16:38:02","date_gmt":"2015-12-15T13:38:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=270409"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=270409","title":{"rendered":"XML, XPath \u0438 \u0442\u0440\u043e\u0439\u043d\u0430\u044f \u043f\u0435\u0447\u0430\u043b\u044c \u0441 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c\u044e"},"content":{"rendered":"<p>       \u041f\u043e\u0435\u0437\u0434\u043a\u0430 \u0432 \u0414\u043d\u0435\u043f\u0440\u043e\u043f\u0435\u0442\u0440\u043e\u0432\u0441\u043a, \u0445\u0440\u043e\u043d\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043d\u0435\u0434\u043e\u0441\u044b\u043f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 \u043f\u0430\u0440\u0443 \u0434\u043d\u0435\u0439, \u043d\u043e \u043f\u0440\u0438\u044f\u0442\u043d\u044b\u0439 \u0431\u043e\u043d\u0443\u0441 \u043f\u043e \u043f\u0440\u0438\u0435\u0437\u0434\u0443 \u0432 \u0425\u0430\u0440\u044c\u043a\u043e\u0432\u2026 \u0417\u0438\u043c\u043d\u044f\u044f \u043f\u043e\u0433\u043e\u0434\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043c\u043e\u0442\u0438\u0432\u0438\u0440\u0443\u0435\u0442 \u043d\u0430 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0447\u0435\u0433\u043e-\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0433\u043e\u2026<\/p>\n<p>  \u0414\u0430\u0432\u043d\u043e \u0443\u0436\u0435 \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u0440\u043e \u00ab\u043f\u043e\u0434\u0432\u043e\u0434\u043d\u044b\u0435 \u043a\u0430\u043c\u043d\u0438\u00bb \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 <i>XML<\/i> \u0438 <i>XQuery<\/i>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u044c \u043a \u043a\u0430\u0432\u0435\u0440\u0437\u043d\u044b\u043c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430\u043c \u0441 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c\u044e.<\/p>\n<p>  \u0412 \u043e\u0431\u0449\u0435\u043c, \u0434\u043b\u044f \u0442\u0435\u0445 \u043a\u0442\u043e \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 <i>SQL Server<\/i>, <i>XQuery<\/i> \u0438 \u043b\u044e\u0431\u0438\u0442 \u043f\u0430\u0440\u0441\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0438\u0437 <i>XML<\/i> \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442\u0441\u044f \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0441 \u043d\u0438\u0436\u0435\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u043e\u043c\u2026<br \/>  <a name=\"habracut\"><\/a> <br \/>  \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0439 <i>XML<\/i> \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0431\u0443\u0434\u0435\u043c \u043f\u0440\u043e\u0432\u043e\u0434\u0438\u0442\u044c \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u044b:<\/p>\n<pre><code class=\"sql\">USE AdventureWorks2012 GO  IF OBJECT_ID('tempdb.dbo.##temp') IS NOT NULL     DROP TABLE ##temp GO  SELECT val = (     SELECT           [@obj_id] = o.[object_id]         , [@obj_name] = o.name         , [@sch_name] = s.name         , (             SELECT i.name, i.column_id, i.user_type_id, i.is_nullable, i.is_identity             FROM sys.all_columns i             WHERE i.[object_id] = o.[object_id]             FOR XML AUTO, TYPE         )     FROM sys.all_objects o     JOIN sys.schemas s ON o.[schema_id] = s.[schema_id]     WHERE o.[type] IN ('U', 'V')     FOR XML PATH('obj'), ROOT('objects') ) INTO ##temp  DECLARE @sql NVARCHAR(4000) = 'bcp &quot;SELECT * FROM ##temp&quot; queryout &quot;D:\\sample.xml&quot; -S ' + @@servername + ' -T -w -r -t' EXEC sys.xp_cmdshell @sql  IF OBJECT_ID('tempdb.dbo.##temp') IS NOT NULL     DROP TABLE ##temp <\/code><\/pre>\n<p>  \u0414\u043b\u044f \u0442\u0435\u0445, \u0443 \u043a\u043e\u0433\u043e <i>xp_cmdshell<\/i> \u043e\u0442\u043a\u043b\u044e\u0447\u0435\u043d\u0430 \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c:<\/p>\n<pre><code class=\"sql\">EXEC sp_configure 'show advanced options', 1 GO RECONFIGURE GO EXEC sp_configure 'xp_cmdshell', 1 GO RECONFIGURE GO <\/code><\/pre>\n<p>  \u0412 \u0438\u0442\u043e\u0433\u0435 \u043f\u043e \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u043c\u0443 \u043f\u0443\u0442\u0438 \u0443 \u043d\u0430\u0441 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u043d \u0444\u0430\u0439\u043b \u0441 \u0442\u0430\u043a\u043e\u0439 \u0432\u043e\u0442 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439:<\/p>\n<pre><code class=\"sql\">&lt;objects&gt;   &lt;obj obj_id=&quot;245575913&quot; obj_name=&quot;DatabaseLog&quot; sch_name=&quot;dbo&quot;&gt;     &lt;i name=&quot;DatabaseLogID&quot; column_id=&quot;1&quot; user_type_id=&quot;56&quot; is_nullable=&quot;0&quot; is_identity=&quot;1&quot; \/&gt;     &lt;i name=&quot;PostTime&quot; column_id=&quot;2&quot; user_type_id=&quot;61&quot; is_nullable=&quot;0&quot; is_identity=&quot;0&quot; \/&gt;     &lt;i name=&quot;DatabaseUser&quot; column_id=&quot;3&quot; user_type_id=&quot;256&quot; is_nullable=&quot;0&quot; is_identity=&quot;0&quot; \/&gt;     &lt;i name=&quot;Event&quot; column_id=&quot;4&quot; user_type_id=&quot;256&quot; is_nullable=&quot;0&quot; is_identity=&quot;0&quot; \/&gt;     &lt;i name=&quot;Schema&quot; column_id=&quot;5&quot; user_type_id=&quot;256&quot; is_nullable=&quot;1&quot; is_identity=&quot;0&quot; \/&gt;     &lt;i name=&quot;Object&quot; column_id=&quot;6&quot; user_type_id=&quot;256&quot; is_nullable=&quot;1&quot; is_identity=&quot;0&quot; \/&gt;     &lt;i name=&quot;TSQL&quot; column_id=&quot;7&quot; user_type_id=&quot;231&quot; is_nullable=&quot;0&quot; is_identity=&quot;0&quot; \/&gt;     &lt;i name=&quot;XmlEvent&quot; column_id=&quot;8&quot; user_type_id=&quot;241&quot; is_nullable=&quot;0&quot; is_identity=&quot;0&quot; \/&gt;   &lt;\/obj&gt;   ...   &lt;obj obj_id=&quot;1237579447&quot; obj_name=&quot;Employee&quot; sch_name=&quot;HumanResources&quot;&gt;     &lt;i name=&quot;BusinessEntityID&quot; column_id=&quot;1&quot; user_type_id=&quot;56&quot; is_nullable=&quot;0&quot; is_identity=&quot;0&quot; \/&gt;     &lt;i name=&quot;NationalIDNumber&quot; column_id=&quot;2&quot; user_type_id=&quot;231&quot; is_nullable=&quot;0&quot; is_identity=&quot;0&quot; \/&gt;     &lt;i name=&quot;LoginID&quot; column_id=&quot;3&quot; user_type_id=&quot;231&quot; is_nullable=&quot;0&quot; is_identity=&quot;0&quot; \/&gt;     &lt;i name=&quot;OrganizationNode&quot; column_id=&quot;4&quot; user_type_id=&quot;128&quot; is_nullable=&quot;1&quot; is_identity=&quot;0&quot; \/&gt;     &lt;i name=&quot;OrganizationLevel&quot; column_id=&quot;5&quot; user_type_id=&quot;52&quot; is_nullable=&quot;1&quot; is_identity=&quot;0&quot; \/&gt;     &lt;i name=&quot;JobTitle&quot; column_id=&quot;6&quot; user_type_id=&quot;231&quot; is_nullable=&quot;0&quot; is_identity=&quot;0&quot; \/&gt;     &lt;i name=&quot;BirthDate&quot; column_id=&quot;7&quot; user_type_id=&quot;40&quot; is_nullable=&quot;0&quot; is_identity=&quot;0&quot; \/&gt;     ...   &lt;\/obj&gt;   ... &lt;\/objects&gt; <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u0447\u043d\u0435\u043c \u0437\u0430\u0431\u043e\u0440\u043d\u044b\u0435 \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u044b\u2026<\/p>\n<p>  \u041a\u0430\u043a \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 <i>XML<\/i>? \u041d\u0430\u0432\u0435\u0440\u043d\u043e\u0435, \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0442\u044c \u0444\u0430\u0439\u043b \u0431\u043b\u043e\u043a\u043d\u043e\u0442\u043e\u043c, \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u0438 \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e\u2026 \u0414\u0443\u043c\u0430\u044e, \u0447\u0442\u043e \u043a\u043e\u0448\u0435\u0440\u043d\u0435\u0435 \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f <i>OPENROWSET<\/i>:<\/p>\n<pre><code class=\"sql\">DECLARE @xml XML SELECT @xml = BulkColumn FROM OPENROWSET(BULK 'D:\\sample.xml', SINGLE_BLOB) x  SELECT @xml <\/code><\/pre>\n<p>  \u041d\u043e \u0442\u0443\u0442 \u0435\u0441\u0442\u044c \u0437\u0430\u0431\u0430\u0432\u043d\u044b\u0439 \u043f\u043e\u0434\u0432\u043e\u0445. \u041a\u0430\u043a \u043e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c, \u0441\u043e\u0432\u043c\u0435\u0449\u0435\u043d\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0438 \u043f\u0430\u0440\u0441\u0438\u043d\u0433\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0438\u0437 <i>XML<\/i> \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u044c \u043a \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u043c\u0443 \u0441\u043d\u0438\u0436\u0435\u043d\u0438\u044e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f <i>obj_id<\/i> \u0438\u0437 \u0440\u0430\u043d\u0435\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430:<\/p>\n<pre><code class=\"sql\">;WITH cte AS  (     SELECT x = CAST(BulkColumn AS XML)     FROM OPENROWSET(BULK 'D:\\sample.xml', SINGLE_BLOB) x ) SELECT t.c.value('@obj_id', 'INT') FROM cte CROSS APPLY x.nodes('objects\/obj') t(c) <\/code><\/pre>\n<p>  \u041d\u0430 \u043c\u043e\u0435\u0439 \u043c\u0430\u0448\u0438\u043d\u0435 \u044d\u0442\u043e\u0442 \u0437\u0430\u043f\u0440\u043e\u0441 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043e\u0447\u0435\u043d\u044c \u0434\u043e\u043b\u0433\u043e:<\/p>\n<pre><code class=\"sql\">(495 row(s) affected) Table 'Worktable'. Scan count 0, logical reads 20788, ..., lob logical reads 7817781, ..., lob read-ahead reads 1022368.  SQL Server Execution Times:    CPU time = 53688 ms,  elapsed time = 53911 ms. <\/code><\/pre>\n<p>  \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u0438 \u043f\u0430\u0440\u0441\u0438\u043d\u0433:<\/p>\n<pre><code class=\"sql\">DECLARE @xml XML SELECT @xml = BulkColumn FROM OPENROWSET(BULK 'D:\\sample.xml', SINGLE_BLOB) x  SELECT t.c.value('@obj_id', 'INT') FROM @xml.nodes('objects\/obj') t(c) <\/code><\/pre>\n<p>  \u0412\u0441\u0435 \u043e\u0442\u0440\u0430\u0431\u043e\u0442\u0430\u043b\u043e \u043e\u0447\u0435\u043d\u044c \u0431\u044b\u0441\u0442\u0440\u043e:<\/p>\n<pre><code class=\"sql\">(1 row(s) affected) Table 'Worktable'. Scan count 0, logical reads 7, ..., lob logical reads 2691, ..., lob read-ahead reads 344.  SQL Server Execution Times:    CPU time = 15 ms,  elapsed time = 51 ms.  (495 row(s) affected)  SQL Server Execution Times:    CPU time = 47 ms,  elapsed time = 125 ms. <\/code><\/pre>\n<p>  \u0422\u0430\u043a \u0432 \u0447\u0435\u043c \u0436\u0435 \u0431\u044b\u043b\u0430 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430? \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u043e\u0430\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043f\u043b\u0430\u043d \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/59f\/fc4\/eec\/59ffc4eec3d74bb4b7275006dd6446c0.png\"\/><\/p>\n<p>  \u041a\u0430\u043a \u043e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c, \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u043a\u0440\u043e\u0435\u0442\u0441\u044f \u0432 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0442\u0438\u043f\u043e\u0432, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u0442\u0430\u0440\u0430\u0439\u0442\u0435\u0441\u044c \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044e <i>nodes<\/i> \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u0432 \u0442\u0438\u043f\u0435 <i>XML<\/i>.<\/p>\n<p>  \u0414\u0430\u043b\u0435\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0442\u0438\u043f\u0438\u0447\u043d\u0443\u044e \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0438 \u043f\u0430\u0440\u0441\u0438\u043d\u0433\u0435 \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044e\u2026 \u0412 \u0442\u0430\u043a\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043c\u043d\u0438\u0442\u044c, \u0447\u0442\u043e <i>SQL Server<\/i> \u043d\u0435 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u0443\u0435\u0442 \u0432\u044b\u0437\u043e\u0432\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 <i>XML<\/i>.<\/p>\n<p>  \u0414\u043b\u044f \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e\u0441\u0442\u0438 \u0441\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u043a\u0430\u0436\u0443, \u0447\u0442\u043e \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u044f <i>value<\/i> \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430 \u0434\u0432\u0430\u0436\u0434\u044b: <\/p>\n<pre><code class=\"sql\">SELECT t.c.value('@obj_id', 'INT') FROM @xml.nodes('objects\/obj') t(c) WHERE t.c.value('@obj_id', 'INT') &lt; 0 <\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"sql\">(404 row(s) affected)  SQL Server Execution Times:    CPU time = 116 ms,  elapsed time = 120 ms. <\/code><\/pre>\n<p>  \u0414\u0430\u043d\u043d\u044b\u0439 \u043d\u044e\u0430\u043d\u0441 \u043c\u043e\u0436\u0435\u0442 \u0441\u043d\u0438\u0436\u0430\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442\u0441\u044f \u0441\u043e\u043a\u0440\u0430\u0449\u0430\u0442\u044c \u0432\u044b\u0437\u043e\u0432\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0439:<\/p>\n<pre><code class=\"sql\">SELECT * FROM (     SELECT id = t.c.value('@obj_id', 'INT')     FROM @xml.nodes('objects\/obj') t(c) ) t WHERE t.id &lt; 0 <\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"sql\">(404 row(s) affected)  SQL Server Execution Times:    CPU time = 62 ms,  elapsed time = 74 ms. <\/code><\/pre>\n<p>  \u041a\u0430\u043a \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043c\u043e\u0436\u043d\u043e \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u0442\u044c \u0435\u0449\u0435 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"sql\">SELECT t.c.value('@obj_id', 'INT') FROM @xml.nodes('objects\/obj[@obj_id &lt; 0]') t(c) <\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"sql\">(404 row(s) affected)  SQL Server Execution Times:    CPU time = 110 ms,  elapsed time = 119 ms. <\/code><\/pre>\n<p>  \u043d\u043e \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0432\u044b\u0438\u0433\u0440\u044b\u0448\u0435 \u043d\u0435 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u044c\u0441\u044f. \u0425\u043e\u0442\u044f \u043f\u043b\u0430\u043d\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0433\u043e\u0432\u043e\u0440\u044f\u0442 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043e\u0431 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/de3\/8d1\/73c\/de38d173c83f499686a1316d8f8fa308.png\"\/><\/p>\n<p>  \u041f\u043e\u043a\u0430\u0437\u0430\u043d\u043e, \u0447\u0442\u043e \u0442\u0440\u0435\u0442\u0438\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441\u0430\u043c\u044b\u0439 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439\u2026 \u041f\u0443\u0441\u0442\u044c \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0435\u0449\u0435 \u043e\u0434\u043d\u0438\u043c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u043c \u043d\u0435 \u0434\u043e\u0432\u0435\u0440\u044f\u0442\u044c <i>QueryCost<\/i>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435\u0439 \u043e\u0446\u0435\u043d\u043a\u043e\u0439.<\/p>\n<p>  \u0418 \u0441\u0430\u043c\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u043d\u0430 \u0437\u0430\u043a\u0443\u0441\u043a\u0443\u2026 \u0415\u0441\u0442\u044c \u0435\u0449\u0435 \u043e\u0434\u043d\u0430 <b>\u041e\u0427\u0415\u041d\u042c<\/b> \u0432\u0430\u0436\u043d\u0430\u044f \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u0438 \u043f\u0430\u0440\u0441\u0438\u043d\u0433\u0435 \u0438\u0437 <i>XML<\/i>. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u043c \u0437\u0430\u043f\u0440\u043e\u0441:<\/p>\n<pre><code class=\"sql\">SELECT       t.c.value('..\/@obj_name', 'SYSNAME')     , t.c.value('@name', 'SYSNAME') FROM @xml.nodes('objects\/obj\/*') t(c) <\/code><\/pre>\n<p>  \u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u043e\u0436\u0435\u0442 \u0443\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0435\u0445, \u043a\u0442\u043e \u0443\u0436\u0435 \u043d\u0438\u043a\u0443\u0434\u0430 \u043d\u0435 \u0442\u043e\u0440\u043e\u043f\u0438\u0442\u0441\u044f:<\/p>\n<pre><code class=\"sql\">(5273 row(s) affected)  SQL Server Execution Times:    CPU time = 66578 ms,  elapsed time = 66714 ms. <\/code><\/pre>\n<p>  \u041f\u043e\u0447\u0435\u043c\u0443 \u044d\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442? <i>SQL Server<\/i> \u0441\u0435\u0440\u0432\u0435\u0440 \u0438\u043c\u0435\u0435\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0432 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u0445 \u0447\u0442\u0435\u043d\u0438\u044f \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0445 \u0443\u0437\u043b\u043e\u0432:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/7d1\/31f\/338\/7d131f338cc34d76b0a57ff60555ab05.png\"\/><\/p>\n<p>  \u041a\u0430\u043a \u0436\u0435 \u043d\u0430\u043c \u0432 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0431\u044b\u0442\u044c? \u0412\u0441\u0435 \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e\u2026 \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u0447\u0442\u0435\u043d\u0438\u0435 \u0441 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 \u0438 \u0432\u044b\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <i>CROSS\/OUTER APPLY<\/i>:<\/p>\n<pre><code class=\"sql\">SELECT       t.c.value('@obj_name', 'SYSNAME')     , t2.c2.value('@name', 'SYSNAME') FROM @xml.nodes('objects\/obj') t(c) CROSS APPLY t.c.nodes('*') t2(c2) <\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"sql\">(5273 row(s) affected)  SQL Server Execution Times:    CPU time = 156 ms,  elapsed time = 184 ms. <\/code><\/pre>\n<p>  \u0415\u0449\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 2 \u0443\u0440\u043e\u0432\u043d\u044f \u0432\u044b\u0448\u0435. \u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0441\u043e \u0447\u0442\u0435\u043d\u0438\u0435\u043c \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0443 \u043c\u0435\u043d\u044f \u043d\u0435 \u0432\u043e\u0441\u043f\u0440\u043e\u0438\u0437\u0432\u0435\u043b\u0430\u0441\u044c:<\/p>\n<pre><code class=\"sql\">USE AdventureWorks2012 GO  DECLARE @xml XML SELECT @xml = (     SELECT           [@obj_name] = o.name         , [columns] = (             SELECT i.name             FROM sys.all_columns i             WHERE i.[object_id] = o.[object_id]             FOR XML AUTO, TYPE         )     FROM sys.all_objects o     WHERE o.[type] IN ('U', 'V')     FOR XML PATH('obj') )  SELECT       t.c.value('..\/..\/@obj_name', 'SYSNAME')     , t.c.value('@name', 'SYSNAME') FROM @xml.nodes('obj\/columns\/*') t(c) <\/code><\/pre>\n<p>  \u0415\u0449\u0435 \u0445\u043e\u0442\u0435\u043b \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u044c \u043e\u0431 \u043e\u0434\u043d\u043e\u0439 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0439 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0438. \u041f\u0440\u043e\u0431\u043b\u0435\u043c \u0441 \u0447\u0442\u0435\u043d\u0438\u0435\u043c \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 <i>OPENXML<\/i> \u043d\u0435 \u0438\u043c\u0435\u0435\u0442:<\/p>\n<pre><code class=\"sql\">DECLARE       @xml XML     , @idoc INT  SELECT @xml = BulkColumn FROM OPENROWSET(BULK 'D:\\sample.xml', SINGLE_BLOB) x  EXEC sys.sp_xml_preparedocument @idoc OUTPUT, @xml   SELECT * FROM OPENXML(@idoc, '\/objects\/obj\/*')  WITH (     name  SYSNAME '..\/@obj_name',      col   SYSNAME '@name' )  EXEC sys.sp_xml_removedocument @idoc <\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"sql\">(5273 row(s) affected)  SQL Server Execution Times:    CPU time = 47 ms,  elapsed time = 137 ms. <\/code><\/pre>\n<p>  \u041d\u043e \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u0434\u0443\u043c\u0430\u0442\u044c, \u0447\u0442\u043e <i>OPENXML<\/i> \u0438\u043c\u0435\u0435\u0442 \u044f\u0432\u043d\u044b\u0435 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 \u043d\u0430\u0434 <i>XQuery<\/i>. \u0423 <i>OPENXML<\/i> \u0442\u043e\u0436\u0435 \u0445\u0432\u0430\u0442\u0430\u0435\u0442 \u043a\u043e\u0441\u044f\u043a\u043e\u0432. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u043c\u044b \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c <i>sp_xml_removedocument<\/i>, \u0442\u043e \u043c\u043e\u0433\u0443\u0442 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0442\u044c \u0441\u0438\u043b\u044c\u043d\u044b\u0435 \u0443\u0442\u0435\u0447\u043a\u0438 \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<p>  \u0412\u0441\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043b\u043e\u0441\u044c \u043d\u0430 <i>SQL Server 2012 SP3 (11.00.6020)<\/i>.<br \/>  \u041f\u043b\u0430\u043d\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0431\u0440\u0430\u043b \u0438\u0437 <a href=\"https:\/\/www.devart.com\/dbforge\/sql\/studio\/sql-query-profiler.html#header\"><i>dbForge<\/i><\/a>.               <\/p>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/273189\/\"> http:\/\/habrahabr.ru\/post\/273189\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>       \u041f\u043e\u0435\u0437\u0434\u043a\u0430 \u0432 \u0414\u043d\u0435\u043f\u0440\u043e\u043f\u0435\u0442\u0440\u043e\u0432\u0441\u043a, \u0445\u0440\u043e\u043d\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043d\u0435\u0434\u043e\u0441\u044b\u043f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 \u043f\u0430\u0440\u0443 \u0434\u043d\u0435\u0439, \u043d\u043e \u043f\u0440\u0438\u044f\u0442\u043d\u044b\u0439 \u0431\u043e\u043d\u0443\u0441 \u043f\u043e \u043f\u0440\u0438\u0435\u0437\u0434\u0443 \u0432 \u0425\u0430\u0440\u044c\u043a\u043e\u0432\u2026 \u0417\u0438\u043c\u043d\u044f\u044f \u043f\u043e\u0433\u043e\u0434\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043c\u043e\u0442\u0438\u0432\u0438\u0440\u0443\u0435\u0442 \u043d\u0430 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0447\u0435\u0433\u043e-\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0433\u043e\u2026<\/p>\n<p>  \u0414\u0430\u0432\u043d\u043e \u0443\u0436\u0435 \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u0440\u043e \u00ab\u043f\u043e\u0434\u0432\u043e\u0434\u043d\u044b\u0435 \u043a\u0430\u043c\u043d\u0438\u00bb \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 <i>XML<\/i> \u0438 <i>XQuery<\/i>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u044c \u043a \u043a\u0430\u0432\u0435\u0440\u0437\u043d\u044b\u043c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430\u043c \u0441 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c\u044e.<\/p>\n<p>  \u0412 \u043e\u0431\u0449\u0435\u043c, \u0434\u043b\u044f \u0442\u0435\u0445 \u043a\u0442\u043e \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 <i>SQL Server<\/i>, <i>XQuery<\/i> \u0438 \u043b\u044e\u0431\u0438\u0442 \u043f\u0430\u0440\u0441\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0438\u0437 <i>XML<\/i> \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442\u0441\u044f \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0441 \u043d\u0438\u0436\u0435\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u043e\u043c\u2026  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-270409","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/270409","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=270409"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/270409\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=270409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=270409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=270409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}