你这样的找法太过复杂,数据库找上下级的数据就不是这样来存储的
解决方案 »
- 求较复杂sql语句?
- 死活装不上Management Studio
- 如何判断一个数据库是不否存在啊?我想判断一下,再创建数据库.谢大家
- 关于64位sql2008的ssis问题
- sql server 2012中的一个数据库日志文件坏了,如何还原数据库?
- UPDAT问题
- 一个SQL关于包含字符串的问题?
- 这样表结构怎么定义,关于联合主键
- 我在服务器级添加一个登录用户,不给任何服务器角色,使之成为A数据库的dbowner,所有数据库角色,为何不能检索A库中的表,竟无select等
- MS SQL SERVER 安装遇到的个问题,谢谢!
- [在线等]如何查询一个给定的字符是否存在于存储有带分隔符的字符串列中
- SQL Server行转换
Create table Org
(OrgID int primary key identity(1,1),
ParentID int default 0,
OrgName nvarchar(50))
insert into Org(ParentID,OrgName) select 0,'机构一'
union all select 0,'机构二'
union all select 1,'机构一一'
union all select 1,'机构一二'
union all select 3,'机构一一一'
with t as
(select OrgID 'x',OrgID 'y' from Org where ParentID=0
union all
select a.x,b.OrgID
from t a
inner join Org b on a.y=b.ParentID)
select a.x 'UserID',
stuff((select ','+rtrim(b.y)
from t b
where b.x=a.x
for xml path('')),1,1,'') 'UserSubOrgID'
from t a
group by a.x/*
UserID UserSubOrgID
----------- -------------------
1 1,3,4,5
2 2(2 row(s) affected)
*/