我有一个表 是类别表
一级分类 和 二级分类都在这个表里面
现在要实现这样的效果
如:
A(一级)
a1(二级)
a2(二级)
B(一级)
b1(二级)
b2(二级)
就像这样一次往下排的应该怎么写呀
名称字段ClassName
相关字段二级ParentID等于一级ID
一级分类 和 二级分类都在这个表里面
现在要实现这样的效果
如:
A(一级)
a1(二级)
a2(二级)
B(一级)
b1(二级)
b2(二级)
就像这样一次往下排的应该怎么写呀
名称字段ClassName
相关字段二级ParentID等于一级ID
解决方案 »
- 约束为何不起作用??
- 请教存储过程判断插入怎么写?
- 我要执行一存储过程,在我设定的时间范围内间隔3分钟执行一次
- 问一个group by的sql
- 各位大哥帮帮忙给想想办法,关于SQLServer2000数据库数据量的传输和服务器硬件性能的监测报告
- 两个表t1,t2,如果t1中的f2为空,则用t2中的f3来代替
- ●●检索记录所需的时间长短与数据表的总记录数没有关系,只跟要检索出来的记录数有关系。这话对不?●●
- sql2000复制 "不能使用空白的对象和列名"
- 关于存储图象文件的问题
- 如何通过SQL Server把文件保存到服务器中的指定目录中或通过SQL Server生成文本文件到指定目录中
- 关于通过存储过程 跨数据库访问权限
- 大家帮我看一这个怎么做删除记录二表的问题
--创建原始表
create table Area (id int identity,Name varchar(10) ,order_by int ,father_ID int )
insert into area values('广东省',2,0)
insert into area values('四川省',2,0)
insert into area values('湖北省',2,0)
insert into area values('东莞市',1,1)
insert into area values('广州市',1,1)
insert into area values('天河区',0,5)
insert into area values('绵阳市',1,2)
insert into area values('武汉市',1,3)
insert into area values('汉口区',0,8)
insert into area values('随州市',1,3)
--创建临时表
create table tmp (id int identity,Name varchar(10) ,order_by int ,father_ID int )
go--创建查询指定节点及其所有子节点的函数
create function f_cid(@ID int) returns @t_level table(id int , level int)
as
begin
declare @level int
set @level = 1
insert into @t_level select @id , @level
while @@ROWCOUNT > 0
begin
set @level = @level + 1
insert into @t_level select a.id , @level
from area a , @t_Level b
where a.father_ID = b.id and b.level = @level - 1
end
return
end
go--创建存储过程并将数据插入临时表
create proc my_proc
as
begin
declare @id as int
set @id = 0
while exists(select 1 from area where order_by = 2 and id > @id)
begin
set @id = (select min(id) from area where order_by = 2 and id > @id)
insert into tmp(Name ,order_by ,father_ID) select a.name,a.order_by ,a.father_id from area a , f_cid(@id) b where a.id = b.id order by a.id
end
end
go
exec my_proc--从临时表提取数据并显示
select case when order_by = 2 then name
when order_by = 1 then ' ' + name
when order_by = 0 then ' ' + name
end name
from tmp order by iddrop function f_cid
drop proc my_proc
drop table area , tmp/*
name
--------------
广东省
东莞市
广州市
天河区
四川省
绵阳市
湖北省
武汉市
汉口区
随州市(所影响的行数为 10 行)
----------------------------------------------------------------------------------------------------------------
create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
insert into tb values('001' , null , '广东省')
insert into tb values('002' , '001' , '广州市')
insert into tb values('003' , '001' , '深圳市')
insert into tb values('004' , '002' , '天河区')
insert into tb values('005' , '003' , '罗湖区')
insert into tb values('006' , '003' , '福田区')
insert into tb values('007' , '003' , '宝安区')
insert into tb values('008' , '007' , '西乡镇')
insert into tb values('009' , '007' , '龙华镇')
insert into tb values('010' , '007' , '松岗镇')
go --查询指定节点及其所有子节点的函数
create function f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int)
as
begin
declare @level int
set @level = 1
insert into @t_level select @id , @level
while @@ROWCOUNT > 0
begin
set @level = @level + 1
insert into @t_level select a.id , @level
from tb a , @t_Level b
where a.pid = b.id and b.level = @level - 1
end
return
end
go --调用函数查询001(广东省)及其所有子节点
select a.* from tb a , f_cid('001') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
001 NULL 广东省
002 001 广州市
003 001 深圳市
004 002 天河区
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇 (所影响的行数为 10 行)
*/ --调用函数查询002(广州市)及其所有子节点
select a.* from tb a , f_cid('002') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
002 001 广州市
004 002 天河区 (所影响的行数为 2 行)
*/ --调用函数查询003(深圳市)及其所有子节点
select a.* from tb a , f_cid('003') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
003 001 深圳市
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇 (所影响的行数为 7 行)
*/ drop table tb
drop function f_cid
{
if (!IsPostBack)
{
AddChild(null,"0");
}
}
/// <summary>
/// 给当前节点添加子节点
/// </summary>
/// <param name="node">当前节点</param>
/// <param name="pid">当前节点的ID</param>
private void AddChild(TreeNode node,string pid)
{
string sql = "select * from 类别表 where 父ID="+pid;
//这个方法自己改下。得到数据集的。
DataSet ds = db.ExecuteDataSet(sql, null, CommandType.Text);
if (ds != null && ds.Tables.Count > 0)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
TreeNode chilenode = new TreeNode();
//指定节点名称
chilenode.Text = dr["name"].ToString();
//给当前节点添加子节点
AddChild(chilenode, dr["sid"].ToString());
if (node == null)
{
TreeView1.Nodes.Add(chilenode);
}
else
{
node.ChildNodes.Add(chilenode);
}
}
}
}
还好着东西PT哥教过我,我知道,不过王SIR给你的是最简单的吧。。 应该!
--或者使用SQL:
--> 测试数据: [tree]
if object_id('[tree]') is not null drop table [tree]
create table [tree] (id int,parentid int,classname varchar(3),parentpath varchar(20))
insert into [tree]
select 1,0,'a',',0,' union all
select 2,0,'b',',0,' union all
select 3,1,'ab',',0,1,' union all
select 4,1,'ac',',0,1,' union all
select 5,2,'ba',',0,2,' union all
select 6,3,'abc',',0,1,3,'
go
;with wsp
as
(
select px=id,lev=cast(1 as varchar),* from tree b where parentid=0
union all
select px=a.px,lev=cast(a.lev+ltrim(row_number() over(order by b.id)) as varchar),b.* from wsp a,tree b where a.id=b.parentid
)
select classname from wsp order by px,lev--结果:
classname
---------
a
ab
abc
ac
b
ba
ID填一级GUID