三、代码实现
步骤1:首先创建一个测试表,这个测试表只包括了pagepath这个字段,等下就会对这个字段进行分析,分解成不同的记录和不同的属性值。
--1,创建测试表
if exists (select * from sysobjects where id = OBJECT_ID('[temp_url]') and OBJECTPROPERTY(id, 'IsUserTable') = 1)
DROP TABLE [temp_url]
CREATE TABLE [temp_url] (
[pagepath] [varchar] (300) NULL)
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/apps/easyurl/pages/index.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/apps/easyurl/pages/reportContent.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/apps/easyurl/pages/sort_edit.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/apps/performancemonitor/pages/OnlinePerformBySystem.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/apps/performancemonitor/pages/PerformanceContent.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/apps/siteMapManage/siteMapManageEdit.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/apps/warning/pages/ErrorWarning.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/apps/warning/pages/ErrorWarningByPage.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/contrib/msg/pages/Error.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/contrib/msg/pages/LogQuery.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/contrib/msg/pages/success.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/contrib/security/pages/ChangePassword.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/contrib/security/pages/CustomerUserRoles.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/contrib/security/pages/editresource.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/contrib/security/pages/EditRole.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/contrib/security/pages/OnlineUserRolesManager.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/contrib/security/pages/resources.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/contrib/security/pages/Roles.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/contrib/security/pages/Users.aspx')
if exists (select * from sysobjects where id = OBJECT_ID('[temp_url]') and OBJECTPROPERTY(id, 'IsUserTable') = 1)
DROP TABLE [temp_url]
CREATE TABLE [temp_url] (
[pagepath] [varchar] (300) NULL)
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/apps/easyurl/pages/index.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/apps/easyurl/pages/reportContent.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/apps/easyurl/pages/sort_edit.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/apps/performancemonitor/pages/OnlinePerformBySystem.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/apps/performancemonitor/pages/PerformanceContent.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/apps/siteMapManage/siteMapManageEdit.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/apps/warning/pages/ErrorWarning.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/apps/warning/pages/ErrorWarningByPage.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/contrib/msg/pages/Error.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/contrib/msg/pages/LogQuery.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/contrib/msg/pages/success.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/contrib/security/pages/ChangePassword.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/contrib/security/pages/CustomerUserRoles.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/contrib/security/pages/editresource.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/contrib/security/pages/EditRole.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/contrib/security/pages/OnlineUserRolesManager.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/contrib/security/pages/resources.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/contrib/security/pages/Roles.aspx')
INSERT [temp_url] ([pagepath]) VALUES ( '/Web/contrib/security/pages/Users.aspx')
步骤2:接着创建一个用来保存这个树形的结构表,[ID]和[ParentID]是少不了的了,[PagePath]是页面路径的字符串,[IsPage]表示这条记录是否是页面,也就是说是否是叶子节点。
--2,创建结果表
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[PageUrlTree](
[ID] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[ParentID] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[PagePath] [varchar](100) COLLATE Chinese_PRC_CI_AS NULL,
[IsPage] [int] NULL CONSTRAINT [DF_PageUrlTree_IsPage] DEFAULT ((0))
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[PageUrlTree](
[ID] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[ParentID] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[PagePath] [varchar](100) COLLATE Chinese_PRC_CI_AS NULL,
[IsPage] [int] NULL CONSTRAINT [DF_PageUrlTree_IsPage] DEFAULT ((0))
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
步骤3:接着创建一个用户函数,[Get_StrArrayStrOfIndex]这个函数是返回一个字符串中第n个分隔符之前的所有字符串,例如:/Web/apps/pages/index.aspx,当执行select [dbo].[Get_StrArrayStrOfIndex]('/Web/apps/pages/index.aspx','/','4'),返回值就是:“/Web/apps/pages/”,这样做就循环对字符串进行处理了。
--3,创建处理函数
-- =============================================
-- Author: <Viajar>
-- Create date: <2010.09.01>
-- Description: <获取字符串的第n个分隔字符串>
-- =============================================
Create function [dbo].[Get_StrArrayStrOfIndex]
(
@str varchar(50), --要分割的字符串
@split varchar(10), --分隔符号
@index int --取第几个元素
)
returns varchar(50)
as
begin
declare @location int
declare @start int
declare @next int
declare @seed int
set @str=ltrim(rtrim(@str))
set @start=1
set @next=1
set @seed=len(@split)
set @location=charindex(@split,@str)
while @location<>0 and @index>@next
begin
set @start=@location+@seed
set @location=charindex(@split,@str,@start)
set @next=@next+1
end
if @location =0 select @location =len(@str)+1
return substring(@str,0,@location+1)
end
-- =============================================
-- Author: <Viajar>
-- Create date: <2010.09.01>
-- Description: <获取字符串的第n个分隔字符串>
-- =============================================
Create function [dbo].[Get_StrArrayStrOfIndex]
(
@str varchar(50), --要分割的字符串
@split varchar(10), --分隔符号
@index int --取第几个元素
)
returns varchar(50)
as
begin
declare @location int
declare @start int
declare @next int
declare @seed int
set @str=ltrim(rtrim(@str))
set @start=1
set @next=1
set @seed=len(@split)
set @location=charindex(@split,@str)
while @location<>0 and @index>@next
begin
set @start=@location+@seed
set @location=charindex(@split,@str,@start)
set @next=@next+1
end
if @location =0 select @location =len(@str)+1
return substring(@str,0,@location+1)
end