SQL Server:使用存储过程将 XML 字段输出为表格数据

发布于 2024-07-23 11:38:15 字数 664 浏览 3 评论 0原文

我使用带有 XML 数据字段的表来存储数据库中所有其他表的审计跟踪。

这意味着同一个 XML 字段具有不同的 XML 信息。 例如,我的表有两条带有 XML 数据的记录,如下所示:

第 1st 记录:

<client>
  <name>xyz</name>
  <ssn>432-54-4231</ssn>
</client>

第 2nd 记录:

<emp>
  <name>abc</name>
  <sal>5000</sal>
</emp>

这是两种示例格式,只有两条记录。 该表实际上在同一字段中具有更多 XML 格式,并且每种格式都有许多记录。

现在我的问题是,在查询时,我需要将这些 XML 格式转换为表格结果集。

我有哪些选择? 查询该表并从中生成报告将是一项常规任务。 我想创建一个存储过程,我可以将其传递给我需要查询“”或“”的存储过程,那么我的存储过程应该返回表格数据。

I am using a table with an XML data field to store the audit trails of all other tables in the database.

That means the same XML field has various XML information. For example my table has two records with XML data like this:

1st record:

<client>
  <name>xyz</name>
  <ssn>432-54-4231</ssn>
</client>

2nd record:

<emp>
  <name>abc</name>
  <sal>5000</sal>
</emp>

These are the two sample formats and just two records. The table actually has many more XML formats in the same field and many records in each format.

Now my problem is that upon query I need these XML formats to be converted into tabular result sets.

What are the options for me? It would be a regular task to query this table and generate reports from it. I want to create a stored procedure to which I can pass that I need to query "<emp>" or "<client>", then my stored procedure should return tabular data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

巴黎夜雨 2024-07-30 11:38:16

一个选项是创建一系列视图,根据您正在执行的关系中的每种类型来显示 aduit 表
例如,

select
    c.value('name','nvarchar(50)') as name,
    c.value('ssn', 'nvarchar(20)') as ssn
from yourtable
cross apply yourxmlcolumn.nodes('/client') as t(c)

您可以对 emp 遵循相同的模式,

您还可以创建一个视图(或计算列)来识别每个 xml 类型,如下所示:

select yourxmlcolumn.value('local-name(/*[1])', 'varchar(100)') as objectType
from yourtable

An option would be to create a series of views that present the aduit table, per type in the relations that you're execpting
for example

select
    c.value('name','nvarchar(50)') as name,
    c.value('ssn', 'nvarchar(20)') as ssn
from yourtable
cross apply yourxmlcolumn.nodes('/client') as t(c)

you could then follow the same pattern for the emp

you could also create a view (or computed column) to identify each xml type like this:

select yourxmlcolumn.value('local-name(/*[1])', 'varchar(100)') as objectType
from yourtable
星軌x 2024-07-30 11:38:16

使用开放 xml 方法

DECLARE @idoc int
EXEC sp_xml_preparedocument @idoc OUTPUT,@xmldoc

SELECT * 进入#test
FROM OPENXML (@idoc, 'xmlfilepath',2)
with (名称 varchar(50),ssn varchar(20)
)

执行 sp_xml_removedocument @idoc

在 #test 中获取数据后
你可以操纵它。

您可以将 diff 数据放入 diff xml 文件中。

Use open xml method

DECLARE @idoc int
EXEC sp_xml_preparedocument @idoc OUTPUT, @xmldoc

SELECT * into #test
FROM OPENXML (@idoc, 'xmlfilepath',2)
WITH (Name varchar(50),ssn varchar(20)
)

EXEC sp_xml_removedocument @idoc

after you get the data in the #test
and you can manipulate this.

you may be put the diff data in diff xml file.

秉烛思 2024-07-30 11:38:15

这有帮助吗?

INSERT INTO @t (data) SELECT '
<client>
  <name>xyz</name>
  <ssn>432-54-4231</ssn>
</client>'

INSERT INTO @t (data) SELECT '
<emp>
  <name>abc</name>
  <sal>5000</sal>
</emp>'
DECLARE @el VARCHAR(20)

SELECT @el = 'client'

SELECT
    x.value('local-name(.)', 'VARCHAR(20)') AS ColumnName,
    x.value('.','VARCHAR(20)') AS ColumnValue
FROM @t
CROSS APPLY data.nodes('/*[local-name(.)=sql:variable("@el")]') a (x)
/*
ColumnName           ColumnValue
-------------------- --------------------
client               xyz432-54-4231
*/

SELECT @el = 'emp'
SELECT
    x.value('local-name(.)', 'VARCHAR(20)') AS ColumnName,
    x.value('.','VARCHAR(20)') AS ColumnValue
FROM @t
CROSS APPLY data.nodes('/*[local-name(.)=sql:variable("@el")]') a (x)
/*
ColumnName           ColumnValue
-------------------- --------------------
emp                  abc5000
*/

does this help?

INSERT INTO @t (data) SELECT '
<client>
  <name>xyz</name>
  <ssn>432-54-4231</ssn>
</client>'

INSERT INTO @t (data) SELECT '
<emp>
  <name>abc</name>
  <sal>5000</sal>
</emp>'
DECLARE @el VARCHAR(20)

SELECT @el = 'client'

SELECT
    x.value('local-name(.)', 'VARCHAR(20)') AS ColumnName,
    x.value('.','VARCHAR(20)') AS ColumnValue
FROM @t
CROSS APPLY data.nodes('/*[local-name(.)=sql:variable("@el")]') a (x)
/*
ColumnName           ColumnValue
-------------------- --------------------
client               xyz432-54-4231
*/

SELECT @el = 'emp'
SELECT
    x.value('local-name(.)', 'VARCHAR(20)') AS ColumnName,
    x.value('.','VARCHAR(20)') AS ColumnValue
FROM @t
CROSS APPLY data.nodes('/*[local-name(.)=sql:variable("@el")]') a (x)
/*
ColumnName           ColumnValue
-------------------- --------------------
emp                  abc5000
*/

无力看清 2024-07-30 11:38:15

xyz432-54-4231 和 abc5000 都不是有效的 XML。

您可以尝试使用 like 语句仅选择一种特定格式,fe:

select * 
from YourTable 
where YourColumn like '[a-z][a-z][a-z][0-9][0-9][0-9][0-9]'

这将匹配 3 个字母后跟 4 个数字。

更好的选择可能是向表中添加一个额外的列,在其中保存日志记录的类型。 然后您可以使用该列来选择所有“emp”或“client”行。

Neither xyz432-54-4231 nor abc5000 is valid XML.

You can try to select only one particular format with a like statement, f.e.:

select * 
from YourTable 
where YourColumn like '[a-z][a-z][a-z][0-9][0-9][0-9][0-9]'

This would match 3 letters followed by 4 numbers.

A better option is probably to add an extra column to the table, where you save the type of the logging. Then you can use that column to select all "emp" or "client" rows.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文