Declare the
cursor before the CTE i.e.
DECLARE A CURSOR FOR
WITH C AS
(
SELECT MC_id
FROM CM_MaterialCategory
WHERE MC_id IN (SELECT id FROM @id)
UNION ALL
SELECT T.MC_id
FROM CM_MaterialCategory AS T INNER JOIN C ON T.MC_parent = C.MC_id
)
SELECT DISTINCT MC_id FROM C
This solution comes with all the usual warnings about cursors - are you sure you can't do this with set-based processing?
And I don't think your code is going to work without a FETCH somewhere