我有一个包含Xml列的表:
SELECT * 
FROM Sqm

xml一行数据的样本将是:
<Sqm version="1.2">
  <Metrics>
    <Metric id="TransactionCleanupThread.RecordUsedTransactionShift" type="timer" unit="µs" count="1" sum="21490"   average="21490"   minValue="73701"    maxValue="73701"                               >73701</Metric>
    <Metric id="TransactionCleanupThread.RefundOldTrans"             type="timer" unit="µs" count="1" sum="184487"  average="184487"  minValue="632704"   maxValue="632704"                              >632704</Metric>
    <Metric id="Database.CreateConnection_SaveContextUserGUID"       type="timer" unit="µs" count="2" sum="7562"    average="3781"    minValue="12928"    maxValue="13006"    standardDeviation="16"     >12967</Metric>
    <Metric id="Global.CurrentUser"                                  type="timer" unit="µs" count="6" sum="4022464" average="670411"  minValue="15"       maxValue="13794345" standardDeviation="1642047">2299194</Metric>
    <Metric id="Global.CurrentUser_FetchIdentityFromDatabase"        type="timer" unit="µs" count="1" sum="4010057" average="4010057" minValue="13752614" maxValue="13752614"                            >13752614</Metric>
  </Metrics>
</Sqm>
对于这些数据,我希望:
SqmId  id                                                   type   unit  count  sum      minValue  maxValue  standardDeviation  Value
=====  ===================================================  =====  ==== …我正在使用通用系统进行报告,该系统从数据库视图中获取数据(SQL Server 2005).在这个视图中,我必须在一行中合并一对多关系中的数据,并在此线程中使用priyanka.sarkar描述的解决方案:将子查询中的多个结果合并为一个逗号分隔值.该解决方案使用SQLXML来合并数据(子查询):
SELECT STUFF(
    (    SELECT ', ' + Name 
         FROM MyTable _in 
         WHERE _in.ID = _out.ID 
         FOR XML PATH('')),        -- Output multiple rows as one xml type value,
                                   -- without xml tags
    1, 2, '')      -- STUFF: Replace the comma at the beginning with empty string
FROM MyTable _out 
GROUP BY ID        -- Removes duplicates
这完全有效(它甚至没有那么重的性能),除了我的数据现在& 通过SQLXML 进行XML编码(&=>  等) - 毕竟我不想要XML数据,我只是用它作为一个技巧 - 因为通用系统我无法对此进行编码以清理它,因此编码数据直接进入报告.我不能在通用系统中使用存储过程,因此CURSOR-merge或COALESCE-ing不是一个选项......
所以我正在寻找的是T-SQL中的一种方式,它允许我再次解码XML,甚至更好:避免SQLXML对其进行编码.显然我可以编写一个存储函数来执行此操作,但我更喜欢内置,更安全的方式...
谢谢你的帮助...
我有一个包含XML列的表:
CREATE TABLE Batches( 
   BatchID int,
   RawXml xml 
)
xml包含以下项目:
<GrobReportXmlFileXmlFile>
   <GrobReport>
       <ReportHeader>
          <OrganizationReportReferenceIdentifier>1</OrganizationReportReferenceIdentifier>
          <OrganizationNumber>4</OrganizationNumber>
       </ReportHeader>
  </GrobReport>
   <GrobReport>
       <ReportHeader>
          <OrganizationReportReferenceIdentifier>2</OrganizationReportReferenceIdentifier>
          <OrganizationNumber>4</OrganizationNumber>
       </ReportHeader>
  </GrobReport>
   <GrobReport>
       <ReportHeader>
          <OrganizationReportReferenceIdentifier>3</OrganizationReportReferenceIdentifier>
          <OrganizationNumber>4</OrganizationNumber>
       </ReportHeader>
  </GrobReport>
   <GrobReport>
       <ReportHeader>
          <OrganizationReportReferenceIdentifier>4</OrganizationReportReferenceIdentifier>
          <OrganizationNumber>4</OrganizationNumber>
       </ReportHeader>
  </GrobReport>
我想要的是生成一个集合,其中包含:
OrganizationReportReferenceNumber  OrganizationNumber
=================================  ==================
1                                  4
2                                  4
3                                  4
4                                  4
我试过了:
SELECT 
    foo.value('/ReportHeader/OrganizationReportReferenceIdentifier') AS ReportIdentifierNumber,
    foo.value('/ReportHeader/OrganizationNumber') AS OrginazationNumber
FROM CDRBatches.RawXML.query('/GrobReportXmlFileXmlFile/GrobReport/ReportHeader') foo
但这不起作用.我试过了:
SELECT 
    foo.value('/ReportHeader/OrganizationReportReferenceIdentifier') AS ReportIdentifierNumber,
    foo.value('/ReportHeader/OrganizationNumber') AS OrginazationNumber
FROM RawXML.nodes('/GrobReportXmlFileXmlFile/GrobReport/ReportHeader') bar(foo)
但这不起作用.所述的XPath表达式
/GrobReportXmlFileXmlFile/GrobReport/ReportHeader
是正确的; 在任何其他xml系统中它返回:
<ReportHeader>
    <OrganizationReportReferenceIdentifier>1</OrganizationReportReferenceIdentifier>
    <OrganizationNumber>4</OrganizationNumber>
</ReportHeader> …我正在尝试从SQL生成XML输出,需要使用UNION语句并命名输出列.
我之前有这个工作,当我不需要使用UNION语句时:
select(
SELECT
    [CompanyName],
    [Address1],
    [Address2],
    [Address3],
    [Town],
    [County],
    [Postcode],
    [Tel],
    [Fax],
    [Email],
    [LocMap]
FROM [UserAccs] FOR XML PATH ('AccountDetails'), root ('Root') 
) as XmlOutput
将输出XML列命名为XmlOutput
我现在正在尝试:
select(
SELECT
    [CompanyName],
    [Address1],
    [Address2],
    [Address3],
    [Town],
    [County],
    [Postcode],
    [Tel],
    [Fax],
    [Email],
    [LocMap]
FROM [UserAccs]
UNION
SELECT
    [CompanyName],
    [Address1],
    [Address2],
    [Address3],
    [Town],
    [County],
    [Postcode],
    [Tel],
    [Fax],
    [Email],
    [LocMap]
FROM [UserAppAccs]
 FOR XML PATH ('AccountDetails'), root ('Root')
) as XmlOutput
但收到错误信息,有没有人知道这方面的方法?
The FOR XML clause is invalid in views, inline functions, derived tables, …我有一个可变大小的XML文档,需要在MSSQL 2008 R2上解析,如下所示:
<data item_id_type="1" cfgid="{4F5BBD5E-72ED-4201-B741-F6C8CC89D8EB}" has_data_event="False">
  <item name="1">
    <field id="{EA032B25-19F1-4C1B-BDDE-3113542D13A5}" type="2">0.506543009706267</field>
    <field id="{71014ACB-571B-4C72-9C9B-05458B11335F}" type="2">-0.79500402346138</field>
    <field id="{740C36E9-1988-413E-A1D5-B3E5B4405B45}" type="2">0.0152649050024924</field>
  </item>
  <item name="2">
    <field id="{EA032B25-19F1-4C1B-BDDE-3113542D13A5}" type="2">0.366096802804087</field>
    <field id="{71014ACB-571B-4C72-9C9B-05458B11335F}" type="2">-0.386642801354842</field>
    <field id="{740C36E9-1988-413E-A1D5-B3E5B4405B45}" type="2">0.031671174184115</field>
  </item>
</data>
.
我需要将其转换为常规表类型数据集,如下所示:
item_name field_id                             field_type  field_value
--------- ------------------------------------ ----------- ---------------
1         EA032B25-19F1-4C1B-BDDE-3113542D13A5 2           0.5065430097062
1         71014ACB-571B-4C72-9C9B-05458B11335F 2           -0.795004023461
1         740C36E9-1988-413E-A1D5-B3E5B4405B45 2           0.0152649050024
2         EA032B25-19F1-4C1B-BDDE-3113542D13A5 2           0.3660968028040
2         71014ACB-571B-4C72-9C9B-05458B11335F 2           -0.386642801354
2         740C36E9-1988-413E-A1D5-B3E5B4405B45 2           0.0316711741841
3         EA032B25-19F1-4C1B-BDDE-3113542D13A5 2           0.8839620369590
3         71014ACB-571B-4C72-9C9B-05458B11335F 2           -0.781459993268
3         740C36E9-1988-413E-A1D5-B3E5B4405B45 2           0.2284423515729 …sql-server performance sqlxml sql-server-2008-r2 cross-apply
是否有机会将MySQL查询的输出直接转换为XML?
我指的是MSSQL与SQL-XML插件之类的东西,例如:
SELECT * FROM table WHERE 1 FOR XML AUTO
返回文本(或精确的MSSQL中的xml数据类型),其中包含根据表中的列生成的XML标记结构.
使用SQL-XML,还可以选择显式定义输出XML结构,如下所示:
SELECT
  1       AS tag,
  NULL    AS parent,
  emp_id  AS [employee!1!emp_id],
  cust_id    AS [customer!2!cust_id],
  region    AS [customer!2!region]
 FROM table
 FOR XML EXPLICIT
它生成如下的XML代码:
<employee emp_id='129'>
   <customer cust_id='107' region='Eastern'/>
</employee>
你有任何线索如何在MySQL中实现这一点吗?
提前感谢您的回答.
如何更改我的查询,以便不会发生此错误:
XML数据类型方法"value"必须是字符串文字
T-SQL代码:
Declare @Count Int = 1 
While(@count <= @j) 
Begin 
insert into mytable 
([Word]) 
Select ([XmlColumn].value(N'word['+Cast(@Count as nvarchar(2))+']/@Entry','nvarchar(max)')) 
    from OtherTable WHERE ID=2
给定表T列:
ID UNIQUEIDENTIFIER
CreatedDate DATETIME
XmlData XML
XmlData的结构如下:
<application>
    <details firstname="first" lastname="last">
        <statement>statement</statement>
    </details>
    <educationHistory>
        <education subject="subject1" />
        <education subject="subject2" />
    </educationHistory>
    <experienceHistory>
        <examiningExperienceHistory>
            <examiningExperience module="module1" />
            <examiningExperience module="module2" />
        </examiningExperienceHistory>
        <teachingExperienceHistory>
            <teachingExperience module="module1" />
            <teachingExperience module="module2" />
        </teachingExperienceHistory>
    </experienceHistory>
</application>
我需要像这样返回一个提取物:
ID Date       FirstName LastName Education    ExaminingExp TeachingExp
-----------------------------------------------------------------------
1  02-10-2012 First     Last     <xmlextract> <xmlextract> <xmlextract>
到目前为止,我有:
SELECT ID,
       CreatedDate [Date],
       XmlData.value('(application/details/@firstname)[1]','varchar(max)') [FirstName],
       XmlData.value('(application/details/@lastname)[1]','varchar(max)') [LastName]
FROM T
我在最后三列中苦苦挣扎.对于每个记录,我需要列出教学/考试经验和教育.有人可以帮忙吗?
我有一个数据表:
itemID          itemLocation    quantity
-------------------------------------------------------
B008KZK44E  COMMITED    1
B008KZK44E  PRIME       1
B008KZK2LE  COMMITED    1
我需要用这个节点结构生成一个xml:
<inventoryItemData>
  <itemID type="FAMILY">B008KZK2LE</itemID>
  <availabilityDetail>
    <itemQuantity>
      <quantity unitOfMeasure="EA">1</quantity>
      <itemLocation>COMMITED</itemLocation>
    </itemQuantity>
  </availabilityDetail>
</inventoryItemData>
<inventoryItemData>
  <itemID type="FAMILY">B008KZK44E</itemID>
  <availabilityDetail>
    <itemQuantity>
      <quantity unitOfMeasure="EA">1</quantity>
      <itemLocation>COMMITED</itemLocation>
    </itemQuantity>
  </availabilityDetail>
  <availabilityDetail>
    <itemQuantity>
      <quantity unitOfMeasure="EA">1</quantity>
      <itemLocation>PRIME</itemLocation>
    </itemQuantity>
  </availabilityDetail>
</inventoryItemData>
我越接近这个:
SELECT 
   'itemID' AS 'itemID/@type',
   itemID AS 'itemID',
   '' AS 'availabilityDetail',
   '' AS 'availabilityDetail/itemQuantity',
   'EA' AS 'availabilityDetail/itemQuantity/quantity/@unitOfMeasure',
   quantity AS 'availabilityDetail/itemQuantity/quantity',
   itemLocation AS 'availabilityDetail/itemQuantity/itemLocation'
FROM TABLE
FOR XML PATH ('inventoryItemData')
我很感激任何解决方案.
谢谢.
TLDR;
我试图使用SQLXML Bulk Loader(4.0)来加载看起来像这样的XML;
<?xml version = "1.0" encoding = "UTF-8"?>
<CarSales>
 <Client>
  <ID >3</ID>
  <ClientName>John Smith3</ClientName>
  <Country name="Colombia"/>
 </Client>
 <Client>
  <ID>7</ID>
  <ClientName>Slow Sid</ClientName>
  <Country name="Bolivia"/>
 </Client>
 <Client>
  <ID>10</ID>
  <ClientName>Missing Town</ClientName>
  <Country name="Argentina"/>
 </Client>
</CarSales>
我希望在我正在使用的唯一表的列中捕获Country name属性("Client_XMLBulkLoad",它还包含客户端名称和ID).那可能吗?
更多细节:
这是我到目前为止对应的XSD("Country"注释掉了)
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sql = "urn:schemas-microsoft-com:mapping-schema">
 <xsd:element name = "CarSales" sql:is-constant = "1" >
  <xsd:complexType>
   <xsd:sequence>
    <xsd:element name = "Client" sql:relation="Client_XMLBulkLoad"
          maxOccurs = "unbounded">
      <xsd:complexType>
       <xsd:sequence>
        <xsd:element name = "ID" type = "xsd:integer" 
                     sql:field = "ID" />
        <xsd:element name = "ClientName" …sqlxml ×10
sql-server ×7
xml ×6
sql ×4
t-sql ×4
cross-apply ×1
database ×1
mysql ×1
performance ×1
vbscript ×1
xml-parsing ×1
xpath ×1