这是在 SQL Server 2008 中存储 XML 数据的好方法吗?

发布于 2024-12-11 04:17:47 字数 1091 浏览 0 评论 0原文

我正在对使用 SQL Server 2008 以以下结构存储数据进行可行性研究:

-MYTABLE

|ID|RECORDS|BRANCH_OFFICE|MONTH|YEAR

RECORDS 列具有 XML 数据类型,它看起来像:

<visits>
  <visit id="000112233">
    <costumer>Mr. One Costumer</costumer>
    <date>2011-02-10</date>
    <employee>MAT01234</employee>
  </visit>
  <visit id="000112234">
    <costumer>Mr. Another Costumer</costumer>
    <date>2011-02-12</date>
    <employee>MAT01235</employee>
  </visit>
  <visit id="000112235">
    <costumer>Mr. Some Costumer</costumer>
    <date>2011-02-12</date>
    <employee>MAT01234</employee>
  </visit>
  <visit id="000112236">
    <costumer>Mr. Some Costumer</costumer>
    <date>2011-02-15</date>
    <employee>MAT01235</employee>
  </visit>
</visits>

我打算使用 xquery 查询 xml 列,这是一个存储这些数据的好方法?

如果我想获取 2011-10 年间发生的访问列表,添加一个带有“日期”标签与现在之间天数差异的标签,怎么可能呢?

I'm doing a feasibility study for storing data in the following structure with SQL Server 2008:

-MYTABLE

|ID|RECORDS|BRANCH_OFFICE|MONTH|YEAR

The RECORDS column has the XML data type and it will look like:

<visits>
  <visit id="000112233">
    <costumer>Mr. One Costumer</costumer>
    <date>2011-02-10</date>
    <employee>MAT01234</employee>
  </visit>
  <visit id="000112234">
    <costumer>Mr. Another Costumer</costumer>
    <date>2011-02-12</date>
    <employee>MAT01235</employee>
  </visit>
  <visit id="000112235">
    <costumer>Mr. Some Costumer</costumer>
    <date>2011-02-12</date>
    <employee>MAT01234</employee>
  </visit>
  <visit id="000112236">
    <costumer>Mr. Some Costumer</costumer>
    <date>2011-02-15</date>
    <employee>MAT01235</employee>
  </visit>
</visits>

I'm intending to query the xml column with xquery, is this a good way to store this data?

If I want to get the list of visits occurring in 2011-10, adding a tag with the difference of days between the "date" tag and now, how could it be?

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

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

发布评论

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

评论(1

风蛊 2024-12-18 04:17:48

针对您的 XML 的查询可能看起来像这样,我认为您的结构很好。无需执行任何花哨的 XML 操作即可获取所需的信息。

declare @T table
(
  ID int identity primary key,
  Records xml
)

insert into @T values('
<visits>
  <visit id="000112233">
    <costumer>Mr. One Costumer</costumer>
    <date>2011-02-10</date>
    <employee>MAT01234</employee>
  </visit>
  <visit id="000112234">
    <costumer>Mr. Another Costumer</costumer>
    <date>2011-02-12</date>
    <employee>MAT01235</employee>
  </visit>
  <visit id="000112235">
    <costumer>Mr. Some Costumer</costumer>
    <date>2011-02-12</date>
    <employee>MAT01234</employee>
  </visit>
  <visit id="000112236">
    <costumer>Mr. Some Costumer</costumer>
    <date>2011-02-15</date>
    <employee>MAT01235</employee>
  </visit>
</visits>')


select T.ID,
       V.X.value('@id', 'nvarchar(10)') as VisitID,
       V.X.value('costumer[1]', 'nvarchar(50)') as Costumer,
       V.X.value('date[1]', 'date') as [Date],
       V.X.value('employee[1]', 'nvarchar(50)') as Employee
from @T as T
  cross apply T.Records.nodes('/visits/visit') as V(X)

结果:

ID          VisitID    Costumer             Date       Employee
----------- ---------- -------------------- ---------- --------------------
1           000112233  Mr. One Costumer     2011-02-10 MAT01234
1           000112234  Mr. Another Costumer 2011-02-12 MAT01235
1           000112235  Mr. Some Costumer    2011-02-12 MAT01234
1           000112236  Mr. Some Costumer    2011-02-15 MAT01235

A query against your XML could look something like this and I think your structure is just fine. There is no need to do any fancy XML stuff to get the information you need.

declare @T table
(
  ID int identity primary key,
  Records xml
)

insert into @T values('
<visits>
  <visit id="000112233">
    <costumer>Mr. One Costumer</costumer>
    <date>2011-02-10</date>
    <employee>MAT01234</employee>
  </visit>
  <visit id="000112234">
    <costumer>Mr. Another Costumer</costumer>
    <date>2011-02-12</date>
    <employee>MAT01235</employee>
  </visit>
  <visit id="000112235">
    <costumer>Mr. Some Costumer</costumer>
    <date>2011-02-12</date>
    <employee>MAT01234</employee>
  </visit>
  <visit id="000112236">
    <costumer>Mr. Some Costumer</costumer>
    <date>2011-02-15</date>
    <employee>MAT01235</employee>
  </visit>
</visits>')


select T.ID,
       V.X.value('@id', 'nvarchar(10)') as VisitID,
       V.X.value('costumer[1]', 'nvarchar(50)') as Costumer,
       V.X.value('date[1]', 'date') as [Date],
       V.X.value('employee[1]', 'nvarchar(50)') as Employee
from @T as T
  cross apply T.Records.nodes('/visits/visit') as V(X)

Result:

ID          VisitID    Costumer             Date       Employee
----------- ---------- -------------------- ---------- --------------------
1           000112233  Mr. One Costumer     2011-02-10 MAT01234
1           000112234  Mr. Another Costumer 2011-02-12 MAT01235
1           000112235  Mr. Some Costumer    2011-02-12 MAT01234
1           000112236  Mr. Some Costumer    2011-02-15 MAT01235
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文