具有uniqueidentifier的递归CTE必须在锚点中具有值
技术问答
248 人阅读
|
0 人回复
|
2023-09-12
|
;WITH Companies(pkCompanyID,name,parentCompanyID,SomeId) AS SELECT tblCompany.pkCompanyID, tblCompany.name, NULL,--this is a int with no value and it works in the anchor NULL AS SomeId--But this uniqueidentifier do not work why? FROM tblCompany WHERE tblCompany.fkCompToCompID IS NULL UNION ALL SELECT tblCompany.pkCompanyID, tblCompany.name, tblCompany.fkCompToCompID, NEWID() FROM tblCompany JOIN Companies ON tblCompany.fkCompToCompID=Companies.pkCompanyID SELECT * FROM Companies因此,个函数会出错:类型和递归查询公司列 SomeId锚点与递归部件不匹配。我知道我可以通过 NULL AS
2 d4 Z" Y0 Z/ e- v+ W! D, tSomeId替换为 CAST(NULL AS2 r, z, Y; I, R9 n
uniqueidentifier)解决这个问题。但是,当parentCompanyId不需要知道它是一个int为什么锚需要知道它是一个uniqueidentifier?是因为uniqueidentifier是对象,而int是值类型?
( d/ P! S$ ~# y' e0 z2 ~, F % A% x( R' G) g4 V, \( G/ U
解决方案: |
|
|
|
|
|