[转]生成无级树(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
相关文档:
系统环境:Windows 7
软件环境:Visual C++ 2008 SP1 +SQL Server 2005
本次目的:编写一个航空管理系统
这是数据库课程设计的成果,虽然成绩不佳,但是作为我用VC++ 以来编写的最大程序还是传到网上,以供参考。用VC++ 做数据库设计并不容易,但也不是不可能。以下是我的程序界面,后面 ......
rs.open sql,conn 与conn.execute(sql)的区别 收藏
引用“srnld(天涯)”的话:
什么时候用什么?
那就凭个人经验积累的感觉了:
如果只需要一笔带过的碰碰数据库,用execute
如果要对数据库作比较庞杂的操作!则最好用  ......
数据库性能优化涉及到很多方面,在数据库开发时可以通过一些基本的优化技巧提高数据库的性能:
1.原则上为创建的每个表都建立一个主键,主键唯一标识某一行记录,用于强制表的实体完整性。SQL Server 2005 Database Engine 将通过为主键列创建唯一索引来强制数据的唯一性。查询中使用主键时,此索引还可用来对数据进行快 ......
从博客园中看到一篇文章,介绍大软件公司面试时常常会出的两道SQL题(见附录)。
我觉得受益很多,在此之前,我一直觉得,SQL2008似乎提供了这方面的支持,但更低的版本,包括2005,非游标做不出来(水平够菜)。总结心得如下:
1、 强大的group by
1
select stdname,
2
isnull(s ......
TA:
1,WANG
2,ZHANG
4,LI
TB:
1,100
2,200
3,400
1.left join 左连接--以左表为基准,右表中没值的,在结果集中以null值代替。(select * from TA left join TB where TA.ID=TB.ID)
1,WANG,100
2,ZHANG,200
4,NULL
2.right join 右连接--以右表为基准,左表中没值的,在 ......