Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用T-SQL将表转换为具有属性的XML_Sql_Sql Server_Xml_Tsql - Fatal编程技术网

使用T-SQL将表转换为具有属性的XML

使用T-SQL将表转换为具有属性的XML,sql,sql-server,xml,tsql,Sql,Sql Server,Xml,Tsql,我正在尝试转换表: tblCustomAttributeSourceSchema,包含列定义和 TBLLabelAttribute,包含每列的值 为了方便起见,我在这个SQLFIDLE中创建了表: 我想将其转换为一个包含“LabelID”(INT类型-最初来自tbllabeattributes)和“XML_值”(XML类型XML)的表,如下例所示。因此,对于labelID=688,它应该是: <attributes> <attribute attribute_id=

我正在尝试转换表:

  • tblCustomAttributeSourceSchema,包含列定义和
  • TBLLabelAttribute,包含每列的值
为了方便起见,我在这个SQLFIDLE中创建了表:

我想将其转换为一个包含“LabelID”(INT类型-最初来自tbllabeattributes)和“XML_值”(XML类型
XML
)的表,如下例所示。因此,对于labelID=688,它应该是:

 <attributes>
  <attribute attribute_id="1" value="2.00" />
  <attribute attribute_id="2" value="3.00" />
  <attribute attribute_id="3" value="60.00"/>
</attributes>

“属性id”
应设置为
tblCustomAttributeSourceSchema
中的AttributeID,
“值”
应设置为
tbllabeattributes
中的值

如果“TBLLabellattributes”中的属性值为null,则该标签ID的XML中应缺少“attribute”记录


我不太熟悉SQL Server中的XML功能。我正在寻找如何将数据转换成这样的XML。非常感谢您的帮助。

在查询中使用FOR XML:

SELECT *
FROM tblCustomAttributeSourceSchema FOR XML AUTO

SELECT *
FROM tblLabelAttributes FOR XML AUTO
或者,您可以在t-sql代码中创建自己的XML:

SELECT LabelID AS "@LabelID",
       col1 AS "Attributes/col1",
       col2 AS "Attributes/col2"
FROM tblLabelAttributes
FOR XML PATH('LabelID')
给出如下输出:

<LabelID LabelID="688">
   <Attributes>
      <col1>2.00</col1>
      <col2>3.00</col2>
   </Attributes>
</LabelID>

2
3

如果代码与您的小提琴一模一样,您似乎有一些问题:

  • 数据是透视的,因此最好将其取消Pivot
  • 您没有属性的种子编号,因此需要创建该编号
  • 我仍然不知道你如何引用第一个表 我为我的工作做了很多xml解析和创建工作,因此下面是一个示例:

    ; WITH d AS 
      (
      SELECT 
       *
      , ROW_NUMBER() OVER(PARTITION BY LabelId ORDER BY val) AS rwn
      FROM tblLabelAttributes
      UNPIVOT (val FOR col IN (col1, col2, col3)) AS upvt
      )
    , distincts AS 
      (
      SELECT DISTINCT LabelId
      FROM d
      )
    Select 
      LabelId AS "@LabelId"
    , (
      SELECT 
        val AS "@value"
      , rwn AS "@attribute_id"
      FROM d y
      WHERE y.LabelId = x.LabelId
      FOR XML PATH('attribute'), TYPE
      )
    From distincts x
    FOR XML PATH('attributes'), ROOT('ROOT')
    
    一般来说,嵌套选择可以很好地用于xml创建,因为您有时需要显示子节点关系,并且在where子句中执行内部对象到外部对象的连接时,嵌套选择可以很好地实现这一点。您还可以通过有时为“for xml…”注释掉某些部分来判断您正在做什么。一个好的xml结构,我通常从最底层的节点构建,然后再上树。这样,如果我需要调试某些东西,我可以注释掉最后一段,并看到xml的一部分存在于多行中。在本例中,如果我注释掉最后一行,则分组将是“LabelId”

    稍微调整的版本,以符合问题中的规格:

    ; WITH d AS 
      (
      SELECT 
       *
      , ROW_NUMBER() OVER(PARTITION BY LabelId ORDER BY val) AS rwn
      FROM tblLabelAttributes
      UNPIVOT (val FOR col IN (col1, col2, col3)) AS upvt
      )
    , distincts AS 
      (
      SELECT DISTINCT LabelId
      FROM d
      )
    Select 
      LabelId AS "@LabelId"
    , (
      SELECT 
        val AS "@value"
      , rwn AS "@attribute_id"
      FROM d y
      WHERE y.LabelId = x.LabelId
      FOR XML PATH('attribute'), TYPE, ROOT('attributes')
      ) 
    From distincts x
    

    从一个到另一个没有外键?您希望如何将它们关联起来?您必须将模式表中的col1:tblCustomAttributeSourceSchema映射到tbllabeattributes中的col1(可能需要取消激活)。是的,您在这两个表上都有多行col1。我想你需要一个来自'AttributeGUID'或'AttributeId'的键才能进入第二张桌子上的'LabelId'或其他什么东西。我知道怎么做。这个需求是专门针对我在查询中提供的XML的。生成元素要简单得多…哇!这太近了。。。如何将其转换为3行(TBLLabellabattributes中每行1行)?您是对的,这不是很好的结构,但这是我继承的东西,我没有控制权。在CTE中取消PIVOT。我真的很感谢你的帮助,但这个查询远远超出了我的工资等级,我可能在几个小时内就知道你在做什么,但你能告诉我你的意思吗?只需从开始的行中删除部分取消PIVOT并自行选择。这应该是不言自明的结果。本质上,它是这样说的:“嘿,你的值(在我的例子中是val),我想引用你的列名(在我的例子中是col)来表示这些值(col1,col2,col3)。”因此,本质上,你得到了一个值列和一个设置了多少字段的指示符列。请在此处阅读更多信息: