求递归查询语句...不要储存过程...谢谢大家了..数据库结构如下:id name pid1 中国 0
2 北京 1
3 山东 1
4 辽宁 1
5 宣武区 2
6 和平门 5
7 青岛 3
8 威海 3
9 大连 4
10 青岛郊区 7
11 大连郊区 9
12 青岛郊区县10求SQL 语句.不要存储过程....查询后的查询结果如下:id name pid
1 中国 0
2 北京 1
5 宣武区 2
6 和平门 5
3 山东 1
7 青岛 3
10 青岛郊区 7
12 青岛郊区县10
8 威海 3
4 辽宁 1
9 大连 4
11 大连郊区 9谢谢大家......
2 北京 1
3 山东 1
4 辽宁 1
5 宣武区 2
6 和平门 5
7 青岛 3
8 威海 3
9 大连 4
10 青岛郊区 7
11 大连郊区 9
12 青岛郊区县10求SQL 语句.不要存储过程....查询后的查询结果如下:id name pid
1 中国 0
2 北京 1
5 宣武区 2
6 和平门 5
3 山东 1
7 青岛 3
10 青岛郊区 7
12 青岛郊区县10
8 威海 3
4 辽宁 1
9 大连 4
11 大连郊区 9谢谢大家......
(
Department nvarchar(50),--部门名称
ParentDept nvarchar(50),--上级部门
DepartManage nvarchar(30)--部门经理
)
insert into #EnterPrise select '技术部','总经办','Tom'
insert into #EnterPrise select '商务部','总经办','Jeffry'
insert into #EnterPrise select '商务一部','商务部','ViVi'
insert into #EnterPrise select '商务二部','商务部','Peter'
insert into #EnterPrise select '程序组','技术部','GiGi'
insert into #EnterPrise select '设计组','技术部','yoyo'
insert into #EnterPrise select '专项组','程序组','Yue'
insert into #EnterPrise select '总经办','','Boss'
--查询部门经理是Tom的下面的部门名称
;with hgo as
(
select *,0 as rank from #EnterPrise where DepartManage='Tom'
union all
select h.*,h1.rank+1 from #EnterPrise h join hgo h1 on h.ParentDept=h1.Department
)
select * from hgo
/*
Department ParentDept DepartManage rank
--------------- -------------------- ----------------------- -----------
技术部 总经办 Tom 0
程序组 技术部 GiGi 1
设计组 技术部 yoyo 1
专项组 程序组 Yue 2
*/
--查询部门经理是GiGi的上级部门名称
;with hgo as
(
select *,0 as rank from #EnterPrise where DepartManage='GiGi'
union all
select h.*,h1.rank+1 from #EnterPrise h join hgo h1 on h.Department=h1.ParentDept
)
select * from hgo
/*
Department ParentDept DepartManage rank
-------------------- ---------------------- ----------- -----------
程序组 技术部 GiGi 0
技术部 总经办 Tom 1
总经办 Boss 2
*/本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ws_hgo/archive/2010/01/31/5274571.aspx
/*
标题:SQL SERVER 2005中查询指定节点及其所有子节点的函数(表格形式显示)
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)
时间:2010-02-02
地点:新疆乌鲁木齐
*/create table tb(id varchar(3) , pid varchar(3) , name nvarchar(10))
insert into tb values('001' , null , N'广东省')
insert into tb values('002' , '001' , N'广州市')
insert into tb values('003' , '001' , N'深圳市')
insert into tb values('004' , '002' , N'天河区')
insert into tb values('005' , '003' , N'罗湖区')
insert into tb values('006' , '003' , N'福田区')
insert into tb values('007' , '003' , N'宝安区')
insert into tb values('008' , '007' , N'西乡镇')
insert into tb values('009' , '007' , N'龙华镇')
insert into tb values('010' , '007' , N'松岗镇')
goDECLARE @ID VARCHAR(3)--查询ID = '001'的所有子节点
SET @ID = '001'
;WITH T AS
(
SELECT ID , PID , NAME
FROM TB
WHERE ID = @ID
UNION ALL
SELECT A.ID , A.PID , A.NAME
FROM TB AS A JOIN T AS B ON A.PID = B.ID
)
SELECT * FROM T ORDER BY 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 行受影响)
*/--查询ID = '002'的所有子节点
SET @ID = '002'
;WITH T AS
(
SELECT ID , PID , NAME
FROM TB
WHERE ID = @ID
UNION ALL
SELECT A.ID , A.PID , A.NAME
FROM TB AS A JOIN T AS B ON A.PID = B.ID
)
SELECT * FROM T ORDER BY ID
/*
ID PID NAME
---- ---- ----------
002 001 广州市
004 002 天河区(2 行受影响)
*/--查询ID = '003'的所有子节点
SET @ID = '003'
;WITH T AS
(
SELECT ID , PID , NAME
FROM TB
WHERE ID = @ID
UNION ALL
SELECT A.ID , A.PID , A.NAME
FROM TB AS A JOIN T AS B ON A.PID = B.ID
)
SELECT * FROM T ORDER BY ID
/*
ID PID NAME
---- ---- ----------
003 001 深圳市
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇(7 行受影响)
*/drop table tb
create table #Info
(
ID char(3),
PID char(3),
[Name] varchar(20)
)
insert into #Info select '001',null,'山东省'
union all select '002','001','烟台市'
union all select '004','002','招远市'
union all select '003','001','青岛市'
union all select '005',null,'四会市'
union all select '006','005','清远市'
union all select '007','006','小分市'
create table #TT
(
ID char(3),
[Level] int
)
declare @level int
set @level=0
insert into #TT select ID,@level from #Info where PID is null
--select * from #TT
while @@rowcount>0
begin
set @level=@level+1
insert into #TT select I.ID,@level from #Info I,#TT T where I.PID=T.ID and [Level]=@level-1
end
select I.* from #Info I join #TT T on I.ID=T.ID order by T.[level]
ID PID Name
---- ---- --------------------
001 NULL 山东省
005 NULL 四会市
002 001 烟台市
003 001 青岛市
006 005 清远市
004 002 招远市
007 006 小分市
(7 行受影响)
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ws_hgo/archive/2009/07/24/4377428.aspx
存储过我不太明白..能写成标准SQL 语句吗...谢了.....
上面的是用sql实现的
下面用asp.net的方法namespace XR.Web
{
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.Sql;
using System.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
DataTable Tables = new DataTable();
DataColumn DC = new DataColumn();
DataRow DR;
ProductCategoryBLL Category = new ProductCategoryBLL();
protected void Page_Load(object sender, EventArgs e)
{
DataTable DT = Category.GetDataTable("YLProductCategory", 0);
CreateDataTable();
GetTree(DT, 0, 0);
DropDownList1.DataSource = Tables;
DropDownList1.DataTextField = "Title";
DropDownList1.DataValueField = "ID";
DropDownList1.DataBind();
}
public void GetTree(DataTable DT,int PID,int Depth)
{
DataView DV = DT.DefaultView;
DV.RowFilter = "ParentID=" + PID;
string str = string.Empty;
if (Depth == 0)
{
str = "";
}
else if (Depth == 1)
{
str = "├";
}
else
{
for (int i = 0; i < Depth; i++)
{
str =str+"├" + "" + "-";
}
}
foreach (DataRowView Drv in DV)
{
int ID = int.Parse(Drv["ID"].ToString());
string Title = Drv["Title"].ToString();
int ParentID = int.Parse(Drv["ParentID"].ToString());
int Dep = int.Parse(Drv["Depth"].ToString());
DR = Tables.NewRow();
DR["ID"] = ID;
DR["Title"] = str + Title;
DR["ParentID"] = ParentID;
DR["Depth"] = Dep;
Tables.Rows.Add(DR);
int n = Dep;
n++;
GetTree(DT,ID, n);
}
}
public void CreateDataTable()
{
DC = new DataColumn();
DC.ColumnName = "ID";
DC.DataType = System.Type.GetType("System.Int32");
Tables.Columns.Add(DC);
DC = new DataColumn();
DC.ColumnName = "Title";
DC.DataType = System.Type.GetType("System.String");
Tables.Columns.Add(DC);
DC = new DataColumn();
DC.ColumnName = "ParentID";
DC.DataType = System.Type.GetType("System.Int32");
Tables.Columns.Add(DC);
DC = new DataColumn();
DC.ColumnName = "Depth";
DC.DataType = System.Type.GetType("System.Int32");
Tables.Columns.Add(DC);
}
}
} 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ws_hgo/archive/2009/11/20/4842026.aspx
1 中国 0
2 北京 1
5 宣武区 2
6 和平门 5
3 山东 1
7 青岛 3
10 青岛郊区 7
12 青岛郊区县 10
8 威海 3
4 辽宁 1
9 大连 4
11 大连郊区 9
以上sql方法,.NET方法
都可以实现你的效果
你都没有认真看我给你的demo
(
select *,0 as rank from lx where id=1
union all
select h.*,h1.rank+1 from lx h join hgo h1 on h.pid=h1.id
)
select * from hgo
结果是这样的...是不是我那里改错了?
(
SELECT ID , PID , cNAME
FROM lx
WHERE ID = 1
UNION ALL
SELECT A.ID , A.PID , A.cNAME
FROM lx AS A JOIN T AS B ON A.PID = B.ID
)
SELECT * FROM T ORDER BY ID
结果如下:1 0 中国
2 1 辽宁省
3 1 河北省
4 2 大连市
5 2 鞍山市
6 3 秦皇岛市
7 3 保定市
8 6 昌梨
9 5 马家屯
/**insert into city
select 1,'中国',0
union
select 2,'北京',1
union
select 3,'山东',1
union
select 4,' 辽宁',1
union
select 5,'宣武区',2
union
select 6,'和平门',5
union
select 7,'青岛',3 union
select 8,'威海',3 union
select 9,'大连',4 union
select 10,'青岛郊区',7 union
select 11,'大连郊区',9 union
select 12,'青岛郊区县',10
**/
关键在于你的无限级,两三级还好说。
LZ的例子 会有一个传值参数。
你知道一条SQL,要递归的话,很难的。
最好用存储过程或者函数来解决吧。
With Tree
as
(
select * from pT
union All
select T.* from pT T
inner join Tree TT on T.id =TT.pid
)
select distinct * from Tree
看看啊是你要的效果?
oracle 中 connect by在sql server中的实现
1 中国 0
2 北京 1
5 宣武区 2
6 和平门 5
3 山东 1
7 青岛 3
10 青岛郊区 7
12 青岛郊区县 10
8 威海 3
4 辽宁 1
9 大连 4
11 大连郊区 9 我想要这样的格式..贴子里的我都试过.改过,都不行.唉..我哭啊我....
2 1 北京市
3 2 海淀区
4 3 黄庄村
5 4 小张公社
6 1 河北省
7 6 秦皇岛市
8 7 海港区
9 8 李三村
这样一相结构很乱吗? 结构就是这样的.无限递归. 最后要用 SQL 语句查询的结果也是这样的...
我知道lz的意思,在pid相同情况下取一个最小id的数据。所以递归必须一条一条接。但是
在递归公用表表达式 'cte' 的递归部分不允许使用 TOP 运算符。
也不能用group by
崩溃啊!!!我要怎么取最小啊!