sql-server-2005 – 在存储过程中执行CTE操作时获取无效的Object错误?

我在存储过程中执行此操作时遇到错误的Object SubSkillIds错误?任何人都可以告诉我为什么会这样?

我的错误消息如下: –

Msg 208, Level 16, State 1, Procedure
SubSkillDelete, Line 47
Invalid object name ‘SubSkillIds’.

码:

WITH SubSkillIds (SubSkillId) AS
(
   -- Base case
   SELECT
        SubSkillId
   FROM dbo.SubSkill
   WHERE RegressionSubSkillId = @SubSkillId
   UNION ALL
   -- Recursive step
   SELECT
        S.SubSkillId
   FROM dbo.SubSkill S
   WHERE S.RegressionSubSkillId IN (
   SELECT
        SubSkillId
   FROM dbo.SubSkill
   WHERE RegressionSubSkillId = @SubSkillId)
)

SELECT  @SubSkillIdFound = SubSkillId
FROM SubSkill WHERE SubSkillId = @SubSkillId

DELETE FROM SubSkillActivity WHERE SubSkillId = @SubSkillId
DELETE FROM SubSkill WHERE RegressionSubSkillId
IN (SELECT * FROM SubSkillIds)
DELETE FROM SubSkill WHERE SubSkillId = @SubSkillId
最佳答案
CTE仅适用于下一个声明 – 您的SELECT ……

它在以后不再可用 – 这就是为什么引用该CTE的第二个DELETE语句将失败的原因.

如果需要为多个单个语句保留CTE的值,则需要将这些值存储到临时表/表变量中.

转载注明原文:sql-server-2005 – 在存储过程中执行CTE操作时获取无效的Object错误? - 代码日志