我有一个使用table的sql部门的层次结构。我已经包含了Activation& amp;的功能。停用部门。
现在我希望如果有人想要激活孩子,那么应该激活其父母,否则它应该返回-11。
我已经制作了一个程序,但它不适用于@Is_Active flag ='Y',其中提供了这个'Y'应该通过程序激活该子项。
-- Add the parameters for the stored procedure here
ALTER PROCEDURE DepartmentSetStatus(@DEPARTMENT_ID nvarchar(1000), @Is_Active char(1))
--[DepartmentSetStatus] "1",'Y'
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
declare @count int
SET NOCOUNT OFF;
if @Is_Active='Y'
begin
WITH Vals1 AS (
SELECT *
FROM [I_DEPARTMENT]
WHERE DEPARTMENT_ID in (select Data from dbo.Split(@DEPARTMENT_ID,','))
UNION ALL
SELECT t1.*
FROM [I_DEPARTMENT] t1 INNER JOIN
Vals1 v ON t1.DEPARTMENT_ID = v.PARENT_ID
)
select @count=count(*) from Vals1 where is_active = 'N'
if @count>0
begin
RETURN -11
end
end
;
WITH Vals AS (
SELECT *
FROM [I_DEPARTMENT]
WHERE DEPARTMENT_ID in (select Data from dbo.Split(@DEPARTMENT_ID,','))
UNION ALL
SELECT t1.*
FROM [I_DEPARTMENT] t1 INNER JOIN
Vals v ON t1.PARENT_ID = v.DEPARTMENT_ID
)
UPDATE I_Department
SET Is_Active= @Is_Active
WHERE DEPARTMENT_ID IN (select DEPARTMENT_ID from Vals )
END
请建议我如何实现所需的输出。
提前致谢
答案 0 :(得分:0)
我理解的东西是这样的。
SELECT @DepartmentID = DepartmentID FROM Child_Departments WHERE Child_ID = @Child_ID
IF (@DepartmentID IS NULL OR @DepartmentID = '')
BEGIN
PRINT 'The child id does not have DepartmentID'
RETURN
END
SELECT @Is_Active = Is_Active FROM Departments WHERE DepartmentID = @DepartmentID
IF @Is_Active = 'Yes'
UPDATE Child_Departments
SET Is_Active = 'Yes'
WHERE Child_ID = @Child_ID