;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 SomeId”替换为“ CAST(NULL AS uniqueidentifier)”来解决此问题。但是,当parentCompanyId不需要知道它是一个int时,锚为什么需要知道它是一个uniqueidentifier?是因为uniqueidentifier是对象,而int是值类型?
NULL默认情况下被假定为,int这就是为什么您不必显式转换该原因的原因。
NULL
int
你可以从这里看到
SELECT NULL AS FOO INTO BAR; SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME='FOO'AND TABLE_NAME='BAR';