使用 uniqueidentifier 的错误记录计数并在 TSQL 中将 xml 路径转换为 ​​nvarchar

发布于 2024-12-21 01:45:55 字数 1443 浏览 1 评论 0原文

以下是一些示例记录

ID(UniqueIdentifier)                 UserId title   description version     parentid    
8362AB94-946F-4092-97C2-ABD957CC2E20    3   title   desc    0   0   
C4E3FCC1-49B5-4310-931F-B8EE48B3F38E    3   title2  desc2   0   1           8362AB94-946F-4092-97C2-ABD957CC2E20
C622113B-C6E6-4ED1-B727-E4F864643CD2    3   title3  desc3   0   2           8362AB94-946F-4092-97C2-ABD957CC2E20

记录表示文本数据的分层编辑。例如,在保存对 ParentBoard 的编辑时,会生成一个新条目,并具有增量版本号,即 12(这一切都发生在应用程序上)。 这里感兴趣的列是

ID - Unique Identifier

ParentId - NVARCHAR(50)

我正在尝试构建一个 xml,如下所示

<Edits>
  <Id>314BA459-3749-4BC1-8AA4-E72B882539F5</Id>
  <Title>Parent Title</Title>
  <Description>Parent Description</Description>
  <Version>0</Version>
  <ParentId></ParentId>
  <SubBoardCount>2</SubBoardCount>
</Edits>

查询

SELECT
            Id  'Id',
            Title   'Title',
            [Description]   'Description',
            Version     'Version',
            ParentId    'ParentId',
            (select COUNT(*) WHERE ParentId =  (CAST(Id AS NVARCHAR(50)))) 'SubBoardCount',
    FROM    Edits
    WHERE ParentId IS NULL OR ParentId = ''
    FOR XML PATH('Edits')

问题

为什么我最终得到的 SubBoardCount 为 0 而不是 2?

Here are some sample records

ID(UniqueIdentifier)                 UserId title   description version     parentid    
8362AB94-946F-4092-97C2-ABD957CC2E20    3   title   desc    0   0   
C4E3FCC1-49B5-4310-931F-B8EE48B3F38E    3   title2  desc2   0   1           8362AB94-946F-4092-97C2-ABD957CC2E20
C622113B-C6E6-4ED1-B727-E4F864643CD2    3   title3  desc3   0   2           8362AB94-946F-4092-97C2-ABD957CC2E20

Records represent a hierarchical edits of textual data. Such as , on saving a edit of ParentBoard a new entry is made and with incremental version number i.e 1,2(this all happens on application).
The columns of interest here are

ID - Unique Identifier

ParentId - NVARCHAR(50)

I am trying to build a xml as follows

<Edits>
  <Id>314BA459-3749-4BC1-8AA4-E72B882539F5</Id>
  <Title>Parent Title</Title>
  <Description>Parent Description</Description>
  <Version>0</Version>
  <ParentId></ParentId>
  <SubBoardCount>2</SubBoardCount>
</Edits>

Query

SELECT
            Id  'Id',
            Title   'Title',
            [Description]   'Description',
            Version     'Version',
            ParentId    'ParentId',
            (select COUNT(*) WHERE ParentId =  (CAST(Id AS NVARCHAR(50)))) 'SubBoardCount',
    FROM    Edits
    WHERE ParentId IS NULL OR ParentId = ''
    FOR XML PATH('Edits')

Problem

Why do i always end up getting SubBoardCount as 0 rather than 2?

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

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

发布评论

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

评论(1

电影里的梦 2024-12-28 01:45:55

因为您的“子查询”没有搜索表。也许您的意思是:

SELECT
    Id 'Id',
    Title 'Title',
    [Description] 'Description',
    Version 'Version',
    ParentId 'ParentId',
    (SELECT COUNT(*) 
     FROM Edits e2 
     WHERE e2.ParentId =  (CAST(e1.Id AS NVARCHAR(50)))) 'SubBoardCount',
FROM    
    Edits e1
WHERE 
    ParentId IS NULL OR ParentId = ''
FOR XML PATH('Edits')

如果没有额外的 FROM Edits e2 子句,子查询中对 ParentIdId 的引用将引用 中的那些列值>e1 (因为我现在已经给它起别名了)并且这些并不相等。

Because your "subquery" isn't searching the table. Perhaps you meant:

SELECT
    Id 'Id',
    Title 'Title',
    [Description] 'Description',
    Version 'Version',
    ParentId 'ParentId',
    (SELECT COUNT(*) 
     FROM Edits e2 
     WHERE e2.ParentId =  (CAST(e1.Id AS NVARCHAR(50)))) 'SubBoardCount',
FROM    
    Edits e1
WHERE 
    ParentId IS NULL OR ParentId = ''
FOR XML PATH('Edits')

Without the extra FROM Edits e2 clause, the references to ParentId and Id within the subquery were referencing those column values from e1 (as I've now aliased it) and these aren't ever equal.

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