我试图通过向另一个表中添加一个左外部联接来修改CTE递归语句,但是现在我收到一条错误消息“关键字'As'附近的语法不正确”。加入PartOperations表之前,我没有任何错误,并且代码正确运行。
我的代码:
public function rules()
{
return [
[['avg', 'sd'], 'number'],
];
}
在“ As aa”处将左外部联接添加到PartOperations表后,发生了我的错误:
WITH MyData AS (
(SELECT aa.imaMethodID,
aa.uimaItemNumber,
aa.imaPartID,
aa.impShortDescription,
aa.imaQuantityPerParent,
aa.imrInventoryUnitOfMeasure,
aa.uimpManufacturer,
aa.uimpManufacturePartNo,
aa.imaLevel,
aa.imaMethodAssemblyID,
aa.imaParentAssemblyID,
aa.uimpModPart,
1 AS BOMLevel,
CAST(LTRIM(RTRIM(aa.uimaItemNumber)) AS varchar(250)) AS LevelPath,
CAST(RIGHT('00000'+LTRIM(RTRIM(REPLACE(aa.uimaItemNumber, REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(aa.uimaItemNumber, '0', ''), '1', ''), '2', ''), '3', ''), '4', ''), '5', ''), '6', ''), '7', ''), '8', ''), '9', ''), ''))), 5) +RIGHT('00000'+LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(aa.uimaItemNumber, '0', ''), '1', ''), '2', ''), '3', ''), '4', ''), '5', ''), '6', ''), '7', ''), '8', ''), '9', ''))), 5) AS varchar(250)) AS LevelPathPadded
FROM M1_HB.dbo.AssemblyMaterialCombo
LEFT OUTER JOIN M1_HB.dbo.PartOperations ON AssemblyMaterialCombo.imaMethodID = PartOperations.imoMethodID
AND AssemblyMaterialCombo.imaMethodAssemblyID = PartOperations.imoMethodAssemblyID) AS aa
WHERE ((aa.imaMethodRevisionID='')
AND (aa.imaParentAssemblyID = 0)
AND (aa.imaMethodID='1BC10200')
AND (aa.imaPartID NOT LIKE 'ZBL%'))
UNION ALL
(SELECT ab.imaMethodID,
ab.uimaItemNumber,
ab.imaPartID,
ab.impShortDescription,
ab.imaQuantityPerParent,
ab.imrInventoryUnitOfMeasure,
ab.uimpManufacturer,
ab.uimpManufacturePartNo,
ab.imaLevel,
ab.imaMethodAssemblyID,
ab.imaParentAssemblyID,
ab.uimpModPart,
BOMLevel+1,
CAST(LevelPath + '| ' + LTRIM(RTRIM(ab.uimaItemNumber)) AS varchar(250)) AS LevelPath,
CAST(LevelPathPadded + '| ' +RIGHT('00000'+LTRIM(RTRIM(REPLACE(ab.uimaItemNumber, REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(ab.uimaItemNumber, '0', ''), '1', ''), '2', ''), '3', ''), '4', ''), '5', ''), '6', ''), '7', ''), '8', ''), '9', ''), ''))), 5) +RIGHT('00000'+LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(ab.uimaItemNumber, '0', ''), '1', ''), '2', ''), '3', ''), '4', ''), '5', ''), '6', ''), '7', ''), '8', ''), '9', ''))), 5) AS varchar(250)) AS LevelPathPadded
FROM M1_HB.dbo.AssemblyMaterialCombo
LEFT OUTER JOIN M1_HB.dbo.PartOperations ON AssemblyMaterialCombo.imaMethodID = PartOperations.imoMethodID
AND AssemblyMaterialCombo.imaMethodAssemblyID = PartOperations.imoMethodAssemblyID) AS ab
INNER JOIN MyData s ON ab.imaMethodID = s.imaPartID
AND ('| ' + s.LevelPath + '| ' NOT LIKE '%|' + Cast(LTRIM(RTRIM(ab.uimaItemNumber))AS varchar(250))+ '|%')
WHERE ab.imaMethodRevisionID=''
AND ab.imaParentAssemblyID = 0
AND ab.imaPartID NOT LIKE 'ZBL%' )
SELECT DISTINCT *
FROM MyData OPTION (maxrecursion 0)
它也出现在联盟部分的同一部分。
我发现您不能在递归CTE https://docs.microsoft.com/en-us/sql/t-sql/queries/with-common-table-expression-transact-sql?view=sql-server-2017中拥有左外部联接
但是后来我发现您应该可以使用OUTER APPLY ... SQL Server CTE left outer join
...并且我正在尝试应用它,但似乎无法获取。
我遇到错误:
FROM M1_HB.dbo.AssemblyMaterialCombo
LEFT OUTER JOIN M1_HB.dbo.PartOperations ON AssemblyMaterialCombo.imaMethodID = PartOperations.imoMethodID
AND AssemblyMaterialCombo.imaMethodAssemblyID = PartOperations.imoMethodAssemblyID) AS aa
这是我的代码。
No column name was specified for column 13 of 'MyData'
No column name was specified for column 14 of 'MyData'
No column name was specified for column 15 of 'MyData'
Invalid column name 'LevelPath'
Invalid column name 'BOMLevel'
Invalid column name 'LevelPathPadded'
Statement(s) could not be prepared.