slect 学生
from tb
where where 校长=@校长
from tb
where where 校长=@校长
解决方案 »
- 能否查出数据库对象的修改者和修改时间
- 执行 ALTER FULLTEXT CATALOG 时如果指定 AS DEFAULT 参数,会覆盖现有默认设置吗?
- 使用windows验证进行jdbc连接
- 在线等.问一下联接问题,
- 请问这样功能怎么样完成,熟悉SQL语句的请进,急急!!!!
- 数据重复显示问题
- 总的问题是:远程sql 2005 develop版可以访问,但是不能更新(比如插入、删除)。
- 查询语句的问题
- SQL SERVER 问题!!!!!!!!!!!!!!!!!急
- 一个简单问题,小送分!
- 求救一个Python读取MSSQL的问题
- sql server数据库10万条纪录用什么的计算机做服务器!!,you
from tb
where where 校长='李校长'
set @str='李校长'
select * from tb where where 校长=@str
一个一级主管对应多个二级主管和成员,一个二级主管对应多个三级主管和成员,三级主管对应多个成员...依次类推
一级主管直接管理二级主管,间接管理三级主管
现要查出一级主管管理的所有人
属于SQL的递归查询,现想用存储过程实现
一个一级主管对应多个二级主管和成员,一个二级主管对应多个三级主管和成员,三级主管对应多个成员...依次类推
一级主管直接管理二级主管,间接管理三级主管
现要查出一级主管管理的所有人
属于SQL的递归查询,现想用存储过程实现
SQL2000数据库
SQL2005可以直接用递归查询
create table Tree(nId smallint, NodeName nvarchar(16), pId smallint)
insert Tree select 1, '中国', NULL
union all select 2, '北京', 1
union all select 3, '上海', 1
union all select 4, '湖北', 1
union all select 5, '湖南', 1
union all select 6, '武汉', 4
union all select 7, '孝感', 4
union all select 8, '长沙', 5
union all select 9, '岳阳', 5
union all select 10, '海淀区', 2
union all select 11, '朝阳区', 2
union all select 12, '上地', 10
union all select 13, '西三旗', 10go
create procedure sp_GetNoedsByRootID
@nId smallint
as
begin
with NewTree(nId, NodeName, pId) as
(
select * from Tree where nId=@nId
union all
select T.* from Tree T join NewTree NT on T.pId=NT.nId
)
select NT.nId, NT.NodeName, PT.NodeName as [ParentName]
from NewTree NT join Tree PT on NT.pId=PT.nId
end
gocreate procedure sp_DelNoedsByRootID
@nId smallint
as
begin
declare @sql nvarchar(max)
set @sql='
with NewTree(nId, NodeName, pId) as
(
select * from Tree where nId=@nId
union all
select T.* from Tree T join NewTree NT on T.pId=NT.nId
)
delete Tree
from Tree T join NewTree NT on T.nId=NT.nId
'
exec sp_executesql @sql, N'@nId smallint', @nId
end
go--查找节点及子节点
exec sp_GetNoedsByRootID 2
/*
nId NodeName ParentName
------ ---------------- ----------------
2 北京 中国
10 海淀区 北京
11 朝阳区 北京
12 上地 海淀区
13 西三旗 海淀区(5 行受影响)
*/--删除节点及子节点
exec sp_DelNoedsByRootID 2
select * from Tree
/*
nId NodeName pId
------ ---------------- ------
1 中国 NULL
3 上海 1
4 湖北 1
5 湖南 1
6 武汉 4
7 孝感 4
8 长沙 5
9 岳阳 5(8 行受影响)
*/drop table Tree
drop procedure sp_GetNoedsByRootID, sp_DelNoedsByRootID
create table CompanyMember( EmployeeID int, ManagerID int , EmpName char(10), [level]int)
insert into CompanyMember
select 1, NULL,'Nancy', 1 union all
select 2, 1, 'Andrew', 2 union all
select 3, 1, 'Janet' , 2 union all
select 4, 1, 'Margaret', 2 union all
select 5, 2, 'Steven', 3 union all
select 6, 2, 'Michael', 3 union all
select 7, 3, 'Robert', 3 union all
select 8, 3, 'Laura', 3 union all
select 9, 3, 'Ann', 3 union all
select 10, 4, 'Ina', 3
---------------------------------------------------------
create procedure GetCompanyRelation @level smallint
as
begin
With TableEmployee (EmployeeID , ManagerID , EmpName , [level],ManagerName)
--EmployeeID :所有员工(包括主管)ID,ManagerID :主管ID,EmpName :员工名,ManagerName:主管名
--[level]:所在的领导层
as
(
select EmployeeID , ManagerID , EmpName , [level] ,(case when [level]=1 then NULL else EmpName end) as ManagerName
from CompanyMember
where [level]=1
union all
select C.EmployeeID , C.ManagerID ,C. EmpName , C.[level],T. EmpName as ManagerName
from CompanyMember C
join TableEmployee T
on T.EmployeeID=C.ManagerID
)
select * from TableEmployee
end
-------------------------------------------------------------
exec GetCompanyRelation 1--求第一层下所有人员信息
-------------------------------------------------------
EmployeeID ManagerID EmpName level ManagerName
--------------------------------------------------------------
1 NULL Nancy 1 NULL
2 1 Andrew 2 Nancy
3 1 Janet 2 Nancy
4 1 Margaret 2 Nancy
10 4 Ina 3 Margaret
7 3 Robert 3 Janet
8 3 Laura 3 Janet
9 3 Ann 3 Janet
5 2 Steven 3 Andrew
6 2 Michael 3 Andrew
来一个简单明了的
if object_id('tb')>0 drop table tb
create table tb(id int ,sname nvarchar(10),parentid int)insert tb values (1,'abc1',0)
insert tb values(2,'abc2',1)
insert tb values (3,'abc3',1)
insert tb values(4,'abc4',3)
insert tb values(5,'abc5',3)
insert tb values(6,'abc6',3)
insert tb values(7,'abc7',5)declare @lev int
set @lev = 0
create table #t(id int ,sname nvarchar(10),parentid int,lev int)
insert #t select *,@lev from tb where id = 3
select * from #t
while @@rowcount > 0
begin
set @lev = @lev + 1
insert #t
select a.*,@lev from tb a join #t b on a.parentid = b.id and b.lev = @lev - 1
end
select * from #tid sname parentid lev
----------- ---------- ----------- -----------
3 abc3 1 0
4 abc4 3 1
5 abc5 3 1
6 abc6 3 1
7 abc7 5 2(5 row(s) affected)