[转]生成无级树(sql函数)
--处理示例
--示例数据
create table tb(ID int,Name varchar(10),ParentID int)
insert tb select 1,'AAAA' ,0
union all select 2,'BBBB' ,0
union all select 3,'CCCC' ,0
union all select 4,'AAAA-1' ,1
union all select 5,'AAAA-2' ,1
union all select 6,'BBBB-1' ,2
union all select 7,'CCCC-1' ,3
union all select 8,'CCCC-2' ,3
union all select 9,'AAAA-1-1',4
go
--创建处理的函数
create function f_id()
returns @re table(id int,level int,sid varchar(8000))
as
begin
declare @l int
set @l=0
insert @re select id,@l,right(10000+id,4)
from tb where ParentID=0
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.id,@l,b.sid+','+right(10000+a.id,4)
from tb a,@re b
where a.ParentID=b.id and b.level=@l-1
end
return
end
go
--调用函数实现查询
select a.*,带缩进的Name=space(b.level*4)+a.Name
from tb a,f_id() b
where a.id=b.id
order by b.sid
go
--删除测试
drop table tb
drop function f_
[转]http://www.cnblogs.com/catxp/articles/381747.html
相关文档:
/***************************************************
作者:herowang(让你望见影子的墙)
日期:2010.1.1
注: 转载请保留此信息
& ......
下列语句部分是Mssql语句,不可以在access中使用。
SQL分类:
DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE)
DML—数据操纵语言(SELECT,DELETE,UPDATE,INSERT)
DCL—数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK)
首先,简要介绍基础语句:
1、说明:创建数据库
CREATE DATABASE data ......
PATINDEX
返回指定表达式中某模式第一次出现的起始位置;如果在全部有效的文本和字符数据类型中没有找到该模式,则返回零。
语法
PATINDEX ( '%pattern%' , expression )
参数
pattern
一个字符串。可以使用通配符,但 pattern 之前和之后必须有 % 字符(搜索第一个和最后一个字符时除外)。pattern 是短字符数 ......
Select CONVERT(varchar(100), GETDATE(), 0) as 05 16 2006 10:57AM
Select CONVERT(varchar(100), GETDATE(), 1) as 05/16/06
Select CONVERT(varchar(100), GETDATE(), 2) as 06.05.16
Select CONVERT(varchar(100), GETDATE(), 3) as 16/05/06
Select CONVERT(varchar(100), GETDATE(), 4) as 16.05.06
Select CON ......
http://blog.csdn.net/java2000_net/archive/2008/04/05/2252640.aspx
http://sqlserver.chinahtml.com/2006/SQL-mssql11432786154012.shtml
http://www.cnblogs.com/garnai/archive/2007/09/19/898221.html
http://tech.ccidnet.com/art/1099/20050223/214511_1.html
http://www.wangchao.net.cn/bbsdetail_43009.html ......