我需要在临时表中获取某些项目的层次结构,因为为其创建层次结构的项目取决于更新后的触发器。因此,我需要在临时表中插入存储过程的结果。之后,我需要一个游标以循环遍历所有结果,然后进行一些更新。这是我的代码。.我不知道为什么,但是它没有进入循环,或者就是我的想法。
ALTER PROCEDURE [dbo].[UPDATE_UDF_VALUES_HIERARCHY] (
@proj_id INT
)
AS
BEGIN
DECLARE @tmpHierarchy TABLE (
proj_id INT ,
parent_wbs_id INT
);
DECLARE @projid INT , @parent_wbs_id INT , @Total INT , @Totaltest INT;
INSERT INTO @tmpHierarchy ( proj_id , parent_wbs_id
)
EXEC GetHierarchyForProject @proj_id;
--INSERT INTO @tmpHierarchy
--Exec GetHierarchyForProject @proj_id
-- to break the match in all scanora
UPDATE udfvalue
SET udf_number = ( SELECT SUM(udf_number)
FROM UDFVALUE
WHERE udf_type_id = 1
AND
proj_id = @proj_id
)
WHERE udf_type_id = 2
AND
proj_id = @proj_id;
DECLARE db_cursor_hierarchy_andreea CURSOR
FOR SELECT *
FROM @tmpHierarchy;
OPEN db_cursor_hierarchy_andreea;
FETCH NEXT FROM db_cursor_hierarchy_andreea INTO @projid , @parent_wbs_id;
IF @proj_id != @projid
BEGIN
IF EXISTS ( SELECT proj_id
FROM udfvalue
WHERE proj_id = @projid
AND
udf_type_id = 2
)
BEGIN
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @Totaltest = SUM(udf_number)
FROM udfvalue
WHERE udf_type_id = 2
AND
fk_id IN ( SELECT proj_id
FROM PROJWBS
WHERE parent_wbs_id = ( SELECT wbs_id
FROM projwbs
WHERE proj_id = @projid
)
);
UPDATE udfvalue
SET udf_number = @Totaltest
WHERE udf_type_id = 2
AND
proj_id = @projid;
END;
END;
FETCH NEXT FROM db_cursor_hierarchy_andreea INTO @projid , @parent_wbs_id;
END;
CLOSE db_cursor_hierarchy_andreea;
DEALLOCATE db_cursor_hierarchy_andreea;
END;
答案 0 :(得分:0)
您的@projid
未初始化,因此为null
。因此,执行if @proj_id!=@projid
时始终返回false,因此您永远不会进入循环。
在if
之前,请确保执行类似SET @projid = -1
的操作。