IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_update_titleinfo]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_update_titleinfo]
GO
CREATE procedure [dbo].[mps_update_titleinfo]
-- Add the parameters for the stored procedure here
@cmpid int
AS
BEGIN
    -- Insert statements for procedure here
    DECLARE @display nvarchar(100) = ''
    DECLARE @length int = 0
    DECLARE @space varchar(20) = '&emsp;'
    DECLARE @i int = 0
    
DECLARE @tem_title table(id int IDENTITY(0,1),tem_id int,tem_level int,tem_title nvarchar(100),tem_display nvarchar(100),PRIMARY KEY(id))
INSERT INTO @tem_title SELECT ot_id,ot_level,ot_title,ot_display FROM mps_title WHERE ot_cmpid = @cmpid ORDER BY ot_order, ot_level
SELECT @length=COUNT(tem_id) FROM @tem_title

DECLARE @t_level int

WHILE @i < @length
BEGIN
SELECT @t_level=tem_level,@display= LTRIM(RTRIM(tem_title)) FROM @tem_title WHERE id=@i
WHILE @t_level > 0
BEGIN
SET @display = @space + @display
SET @t_level = @t_level - 1
END
UPDATE @tem_title SET tem_display=@display WHERE id=@i
SET @i = @i + 1
END
UPDATE mps_title SET ot_display=tem_display FROM mps_title,@tem_title WHERE ot_id=tem_id
END