[英]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);
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.