简体   繁体   English

如何从 XML 到 SQL Server 表中为另一列的多个值读取(分配)tableID?

[英]How to read(assign) tableID for multiple values for one other column from XML into SQL Server table?

Sorry if this question sounds dumb and I am new to XML.对不起,如果这个问题听起来很愚蠢而且我是 XML 的新手。

I have XML metadata that displays the Name of the table and all of its column names.我有显示表名及其所有列名的 XML 元数据。 In this example below, I want to read all 4 column names into 4 rows and also add table name to each iteration of the column name.在下面的这个例子中,我想将所有 4 个列名读入 4 行,并将表名添加到列名的每次迭代中。 But with what I tried, I could only partially achieve the output.但是通过我的尝试,我只能部分实现输出。 It either displays one column name with it's respective table name or no table name but all the columns.它要么显示一个列名及其各自的表名,要么不显示表名但显示所有列。 Any suggestion is greatly appreciated.任何建议都非常感谢。 Thanks in advance!提前致谢!

So far I have tried to materialize a table and then read values using two methods.到目前为止,我已经尝试实现一个表,然后使用两种方法读取值。 I would like to retrieve 4 rows and 2 columns (Table @Name, Field), here table name should repeat "CD_SAP_T000" for all four distinct column names.我想检索 4 行和 2 列(表 @Name,字段),这里的表名应该为所有四个不同的列名重复“CD_SAP_T000”。

Method #1 :方法#1

DECLARE @xml XML = '<ROOT>
  <Table Name="CD_SAP_T000">
    <Field>CCCATEGORY</Field>
    <Field>MANDT</Field>
    <Field>MTEXT</Field>
    <Field>ORT01</Field>
  </Table>
</ROOT>'

SELECT
    ROW_NUMBER() OVER (ORDER BY (SELECT 1)) rowId,
    x.Y.value('@Name[1]', 'VARCHAR(100)') AS TableName,
    x.y.value('.', 'NVARCHAR(200)' ) AS ColumnName
FROM 
    @xml.nodes('//*[text()]') AS x(y)

Result for method #1:方法 #1 的结果:

rowId TableName ColumnName
--------------------------
1     NULL      CCCATEGORY
2     NULL      MANDT
3     NULL      MTEXT
4     NULL      ORT01

Method #2 :方法#2

IF OBJECT_ID('dbo.myTable') IS NOT NULL 
     DROP TABLE dbo.myTable

CREATE TABLE [dbo].[myTable]
(
    [Name] VARCHAR(100) NULL,
    [Field] VARCHAR(100) NULL
);

DECLARE @Z_xml XML

SET @Z_xml = '<ROOT>
  <Table Name="CD_SAP_T000">
    <Field>CCCATEGORY</Field>
    <Field>MANDT</Field>
    <Field>MTEXT</Field>
    <Field>ORT01</Field>
  </Table>
</ROOT>'

INSERT INTO myTable (Name, Field)
    SELECT
        x.mytable.value('@Name[1]', 'VARCHAR(100)'),
        x.mytable.value('Field[1]', 'VARCHAR(100)')
    FROM
        @Z_xml.nodes('//ROOT/Table') AS x ( mytable )

 SELECT * FROM mytable

Result for method #2:方法 #2 的结果:

Name        Field
--------------------------
CD_SAP_T000 CCCATEGORY

One method is to cross apply the "Field" nodes to the respective "Table" nodes.一种方法是将“字段”节点交叉应用于相应的“表”节点。

SELECT t.node.value('(@Name)[1]', 'varchar(MAX)') table_name,
       c.node.value('(text())[1]', 'varchar(MAX)') column_name
       FROM @Z_xml.nodes('/ROOT/Table') t(node)
            CROSS APPLY t.node.nodes('./Field') c(node);

db<>fiddle 数据库<>小提琴

Or you can just use this XQuery to grab the table and column names in one go - without the need for a CROSS APPLY :或者,您可以使用此 XQuery 一次性获取表名和列名 - 无需CROSS APPLY

-- get a list of all <Field> XML elements
SELECT 
    -- grab the parent node's (<Table>) "Name" attribute as the table name
    TableName = XC.value('(../@Name)[1]', 'VARCHAR(100)'),
    -- grab the <Field> text value as column name
    ColumnName = XC.value('(.)[1]', 'VARCHAR(100)')
FROM 
    @Z_xml.nodes('/ROOT/Table/Field') AS XT(XC)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 SQL 服务器从表中的 xml 列中提取值 - How to extract values from xml column in a table using SQL Server 如何从 SQL 服务器中的 XML 列中提取多个标签值 - How to extract multiple tag values from XML column in SQL Server 从XML数据列中将SQL Server中的多个值检索(分解)到表中的同一单元格(行/列交集)中 - Retrieve (Shredding) Multiple Values in SQL Server into same cell (row/ column intersection) in table from XML data column 如何基于SQL Server中当前表列的值使用与其他表不同的列值 - How to use different column values from other table based on value on current table column in SQL server SQL一个表上的多个列从另一表连接到列 - SQL Multiple columns on one table joined column from other table 如何在SQL Server的同一张表中显示XML节点内容从一列到另一列? - How can I display XML node content from one column into another column on the same table in SQL Server? 如何基于另一列SQL Server 2014中的数据更新一列上的值 - How to update the values on one column based on the data from other column SQL Server 2014 根据 SQL Server 中其他表中的值更新多个列 - Update multiple columns based on values from other table in SQL Server 如何为SQL表列分配2个默认值? - How to assign 2 default values to SQL table column? 如何将一个表列行分配给 SQL Server 中的另一个表列值 - How to assign one table column row to another table column value in SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM