IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_update_account_item_info]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_update_account_item_info]
GO
 
 
 
create procedure [dbo].[mps_update_account_item_info]  
 -- Add the parameters for the stored procedure here   
 @cmpid int
AS  
BEGIN  
    -- Insert statements for procedure here  
    
    DECLARE @id int = 0  
    DECLARE @level int = 0  
    DECLARE @fulltime nvarchar(100) = ''  
    DECLARE @fdpid int = 0  
    DECLARE @ffdpid int = 0  
    DECLARE @display nvarchar(100) = ''  
    DECLARE @length int = 0  
    DECLARE @space varchar(20) = '&emsp;'  
    DECLARE @i int = 0  
      
DECLARE @tem_depart table(
id int IDENTITY(0,1),
tem_id int,
tem_level int,
tem_depart nvarchar(100),
tem_fulltime nvarchar(100),
tem_fdpid int,
tem_ffdpid int,
tem_display nvarchar(100),
PRIMARY KEY(id)
)

INSERT INTO @tem_depart 
SELECTai_id,
ai_level,
ai_caption,
ai_full,
ai_faiid,
ai_ffaiid,
ai_display
FROM mps_account_item
WHERE ai_cmpid = @cmpid 
ORDER BY ai_order,
 ai_level

SELECT @length=COUNT(tem_id) FROM @tem_depart  

DECLARE @t_id int  
DECLARE @t_level int  
DECLARE @t_fdpid int  
DECLARE @t_ffdpid int  
DECLARE @t_depart nvarchar(100)  
    
WHILE @i < @length  
BEGIN  
SELECT@t_id=tem_id,
@t_level=tem_level,
@t_fdpid=tem_fdpid,
@t_ffdpid=tem_ffdpid,
@t_depart=tem_depart 
FROM @tem_depart 
WHERE id=@i  

IF @t_level = @level  
BEGIN  
    SET @id = @t_id  
    SET @level = @t_level  
    SET @display = LTRIM(RTRIM(@t_depart))  
    IF @t_level = 0  
    BEGIN  
        SET @fulltime = @t_depart  
    END ELSE  
    BEGIN  
        SET @fulltime = SUBSTRING(@fulltime,1,LEN(@fulltime)-CHARINDEX('/',REVERSE(RTRIM(@fulltime)))) + '/' + @t_depart  
    END  
END 
ELSE IF @t_level > @level  
BEGIN  
    SET @level = @t_level  
    SET @ffdpid = @fdpid  
    SET @fdpid = @id  
    SET @id = @t_id  
    SET @display = LTRIM(RTRIM(@t_depart))  
    SET @fulltime = @fulltime + '/' + @t_depart  
END ELSE  
BEGIN  
    SET @level = @t_level  
    SELECT TOP 1 @fdpid=tem_fdpid,
    @ffdpid=tem_ffdpid,@fulltime=tem_fulltime FROM @tem_depart WHERE id<@i AND tem_level=@level ORDER BY id DESC  
    SET @id = @t_id  
    SET @display = LTRIM(RTRIM(@t_depart))  
    IF @level = 0  
    BEGIN  
        SET @fulltime = @t_depart  
    END ELSE  
    BEGIN  
        SET @fulltime = SUBSTRING(@fulltime,1,LEN(@fulltime)-CHARINDEX('/',REVERSE(RTRIM(@fulltime)))) + '/' + @t_depart  
    END   
END
set @t_level = len(@fulltime) - len(REPLACE(@fulltime, '/', ''));
set @level = @t_level
WHILE @t_level > 0  
BEGIN  
    SET @display = @space + @display  
    SET @t_level = @t_level - 1  
END  
UPDATE @tem_depart 
SET tem_level=@level,
tem_fdpid=@fdpid,
tem_ffdpid=@ffdpid,
tem_display=@display,
tem_fulltime=@fulltime
WHERE id=@i  
SET @i = @i + 1  
END  

UPDATE mps_account_item 
SET ai_display=tem_display,
ai_faiid=tem_fdpid,
ai_ffaiid=tem_ffdpid,
ai_full=tem_fulltime,
ai_level = tem_level
FROMmps_account_item,
@tem_depart 
WHERE ai_id=tem_id  

END