我在DB中有一个包含数据列的字段,但是在一个连续的XML字符串中。 我想从XML中提取数据,并返回普通列。 在这个单一字段中可能有多行数据。
以下是此字段中2个真实的数据示例
示例1:
<DocumentElement>
<AuditTrailDetail>
<TableName>order_header_total</TableName>
<RowID>837</RowID>
<AuditType>Edit</AuditType>
<FieldName>oht_foreign_net</FieldName>
<ValueFrom>65.1600</ValueFrom>
<ValueTo>115.1600</ValueTo>
<EntityName />
<DisplayName />
<Identifier />
</AuditTrailDetail>
<AuditTrailDetail>
<TableName>order_line_item</TableName>
<RowID>2442</RowID>
<AuditType>Edit</AuditType>
<FieldName>oli_qty_required</FieldName>
<ValueFrom>1.0000</ValueFrom>
<ValueTo>2.0000</ValueTo>
<EntityName />
<DisplayName />
<Identifier>61 - test</Identifier>
</AuditTrailDetail>
</DocumentElement>
示例2:
<DocumentElement>
<AuditTrailDetail>
<TableName>order_line_item</TableName>
<RowID>2446</RowID>
<AuditType>Edit</AuditType>
<FieldName>oli_description</FieldName>
<ValueFrom>2 Ply Tissue Masks</ValueFrom>
<ValueTo>2 Ply Tissue Masksdd</ValueTo>
<EntityName />
<DisplayName />
<Identifier>D/D170 - 2 Ply Tissue Masksdd</Identifier>
</AuditTrailDetail>
</DocumentElement>
对于示例1,我想返回2行,如下所示:
对于例2,有一行,我想它如下:
表格中的字段为:
owat_id
owat_audit_type
owat_record_id
owat_record_type
owat_datetime
owat_ud_id
owat_details
XML数据在owat_details
。
假设表名为Audit_Trail
。
如何在SQL查询中直接执行此操作?
答案 0 :(得分:0)
感谢Johnie Karr
我使用XQuery工作了。
我必须进行2次查询并将它们组合在一起,以便将一个字段中的2个单独元素作为单独的行。我确定有一个更清洁的解决方案,但这很有效。
非常感谢:)
SELECT
AuditTrail.owat_record_type,
AuditTrail.owat_audit_type,
AuditTrail.owat_datetime as 'Audit Date',
user_detail.ud_username as 'User',
AuditTrail.owat_details.value('(/DocumentElement/AuditTrailDetail/TableName)[1]','varchar(50)') AS TableName,
AuditTrail.owat_details.value('(/DocumentElement/AuditTrailDetail/RowID)[1]','varchar(50)') AS RowID,
AuditTrail.owat_details.value('(/DocumentElement/AuditTrailDetail/FieldName)[1]','varchar(50)') AS FieldName,
AuditTrail.owat_details.value('(/DocumentElement/AuditTrailDetail/ValueFrom)[1]','varchar(50)') AS ValueFrom,
AuditTrail.owat_details.value('(/DocumentElement/AuditTrailDetail/ValueTo)[1]','varchar(50)') AS ValueTo,
AuditTrail.owat_details.value('(/DocumentElement/AuditTrailDetail/Identifier)[1]','varchar(50)') AS Identifier
FROM
(select
orderwise_audit_trail.owat_id,
orderwise_audit_trail.owat_audit_type,
orderwise_audit_trail.owat_record_id,
orderwise_audit_trail.owat_record_type,
orderwise_audit_trail.owat_datetime,
orderwise_audit_trail.owat_ud_id,
convert(xml, orderwise_audit_trail.owat_details) as owat_details
from
orderwise_audit_trail) as AuditTrail
left join user_detail on user_detail.ud_id = AuditTrail.owat_ud_id
Where AuditTrail.owat_details is not NULL
and (AuditTrail.owat_details.value('(/DocumentElement/AuditTrailDetail/TableName)[1]','varchar(50)')) is not null
UNION ALL
SELECT
AuditTrail.owat_record_type,
AuditTrail.owat_audit_type,
AuditTrail.owat_datetime as 'Audit Date',
user_detail.ud_username as 'User',
AuditTrail.owat_details.value('(/DocumentElement/AuditTrailDetail/TableName)[2]','varchar(50)') AS TableName,
AuditTrail.owat_details.value('(/DocumentElement/AuditTrailDetail/RowID)[2]','varchar(50)') AS RowID,
AuditTrail.owat_details.value('(/DocumentElement/AuditTrailDetail/FieldName)[2]','varchar(50)') AS FieldName,
AuditTrail.owat_details.value('(/DocumentElement/AuditTrailDetail/ValueFrom)[2]','varchar(50)') AS ValueFrom,
AuditTrail.owat_details.value('(/DocumentElement/AuditTrailDetail/ValueTo)[2]','varchar(50)') AS ValueTo,
AuditTrail.owat_details.value('(/DocumentElement/AuditTrailDetail/Identifier)[2]','varchar(50)') AS Identifier
FROM
(select
orderwise_audit_trail.owat_id,
orderwise_audit_trail.owat_audit_type,
orderwise_audit_trail.owat_record_id,
orderwise_audit_trail.owat_record_type,
orderwise_audit_trail.owat_datetime,
orderwise_audit_trail.owat_ud_id,
convert(xml, orderwise_audit_trail.owat_details) as owat_details
from
orderwise_audit_trail) as AuditTrail
left join user_detail on user_detail.ud_id = AuditTrail.owat_ud_id
Where AuditTrail.owat_details is not NULL
and (AuditTrail.owat_details.value('(/DocumentElement/AuditTrailDetail/TableName)[2]','varchar(50)')) is not null
order by 1, 2, 3, 4