技术开发 频道

简单实用SQL脚本:生成站点导航结构

  步骤4:接着创建一个存储过程,[sp_CreatePagePathTree]这个存储过程是对这个方案的整个逻辑进行处理。

--4,处理的存储过程
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:        <Viajar>
-- Create date: <2010.09.15>
-- Description:    <创建站点树形结构>
-- Tips:        <读取PageURL表,生成数据到PageUrlTree表>
-- =============================================
CREATE PROCEDURE sp_CreatePagePathTree

AS
BEGIN

    
--清空记录
    truncate  table PageUrlTree

    
--生成树
    DECLARE @TempPath_Before varchar(
100)
    DECLARE @TempPath varchar(
100)
    DECLARE @pagepath varchar(
50)
    DECLARE @itemCur CURSOR
    
SET @itemCur = CURSOR FOR
        
SELECT pagepath FROM temp_url

    OPEN @itemCur
    FETCH
NEXT FROM @itemCur INTO @pagepath
    
WHILE @@FETCH_STATUS=0
    BEGIN
        
--逻辑处理
        DECLARE @Word NVARCHAR(
2)
        DECLARE @WordAll NVARCHAR(
50)
        DECLARE @count
int
        DECLARE @countBefore
int
        DECLARE @i
int
        DECLARE @ID VARCHAR(
50)
        DECLARE @ParentID VARCHAR(
50)
        DECLARE @MaxID VARCHAR(
50)
        DECLARE @SubID
int
        DECLARE @IsPage
int
        
set @IsPage = 0
        
set @i =1
        
set @Word = '/'
        set @WordAll = @pagepath
        
select @countBefore = len(replace(@WordAll,@Word,@Word+'_'))-len(@WordAll)+1
        select @count = len(replace(@WordAll,@Word,@Word+'_'))-len(@WordAll)+1

        
select @TempPath_Before = dbo.[Get_StrArrayStrOfIndex](@pagepath,'/',1)

        
WHILE @count>0
        begin
            
select @Temppath = dbo.[Get_StrArrayStrOfIndex](@pagepath,'/',@i)
            print '@Temppath:' +@Temppath
            --不存在
            
if not exists (select ID from PageUrlTree where PagePath = @Temppath)
            begin
                
select @ParentID = ID from PageUrlTree where PagePath = @TempPath_Before
                print
'@ParentID:'+@ParentID
                if(@ParentID <> '')
                begin
                    
select @MaxID = max(ID) from PageUrlTree where ParentID = @ParentID and PagePath like @TempPath_Before+'%'
                    if (@MaxID is null)
                        
set @SubID = 100
                    
else
                        
select @SubID = convert(int,substring(@MaxID,len(@MaxID)-2,len(@MaxID)))+1

                    
--如果是.的话就表示是页面
                    
if (charindex('.',@TempPath)>0)
                        set @IsPage =1
                    INSERT PageUrlTree VALUES(@ParentID
+ convert(varchar(10),@SubID),@ParentID,@Temppath,@IsPage)
                
end
                
else
                    INSERT PageUrlTree VALUES(
'100','0',@Temppath,@IsPage)--根节点
            end
        
            
set @i = @i +1
            
set @countBefore = @count
            
set @count = @count-1
            
set @TempPath_Before = @TempPath
        
end

        FETCH
NEXT FROM @itemCur INTO @pagepath
    
END

    CLOSE @itemCur
    DEALLOCATE @itemCur

END
GO

 

  四、总结

  其中这个完全可以使用程序代码来解决,但是既然用了SQL,那就把这个过程记录下来吧,希望对其它人有帮助吧。

0
相关文章