使用 uniqueidentifier 的错误记录计数并在 TSQL 中将 xml 路径转换为 nvarchar
以下是一些示例记录
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 的编辑时,会生成一个新条目,并具有增量版本号,即 1
、2
(这一切都发生在应用程序上)。 这里感兴趣的列是
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为您的“子查询”没有搜索表。也许您的意思是:
如果没有额外的
FROM Edits e2
子句,子查询中对ParentId
和Id
的引用将引用中的那些列值>e1
(因为我现在已经给它起别名了)并且这些并不相等。Because your "subquery" isn't searching the table. Perhaps you meant:
Without the extra
FROM Edits e2
clause, the references toParentId
andId
within the subquery were referencing those column values frome1
(as I've now aliased it) and these aren't ever equal.