求SQL语句

isnewer 2008-04-09 09:48:45
我这里有一张公司结构的表,表结构如下:
corpid varchar(20) 公司ID
deptid varchar(20) 部门ID
parentid varchar(20) 上级部门ID(没有上级部门为-1)

内容如下:
corpid deptid parentid
------------------------------------------
xx A -1
xx A1 A
xx A2 A
xx A3 A
xx A11 A1
xx A12 A1
xx A111 A11
xx A112 A11
xx A31 A3
xx A32 A3
yy A -1
yy B A
.
.
.
如何执行一条SQL语句把corpid为'xx'的所有部门都列出来?
...全文
138 22 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
isnewer 2008-04-09
  • 打赏
  • 举报
回复
又学习了,谢谢!

结帖!
viva369 2008-04-09
  • 打赏
  • 举报
回复
你到底要取什么~~~
isnewer 2008-04-09
  • 打赏
  • 举报
回复
先谢了,测试中...
-狙击手- 2008-04-09
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 isnewer 的回复:]
15楼的同志,说来听听
[/Quote]

看12楼
-狙击手- 2008-04-09
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 isnewer 的回复:]
不会吧,认真点,A1下面还有好多子部门啊.
[/Quote]


楼主我有不认真吗?


你不是要取得 公司xx 部门为A1 的所有下属部门呀?
我那个函数可以满足你的要求呀
isnewer 2008-04-09
  • 打赏
  • 举报
回复
15楼的同志,说来听听
isnewer 2008-04-09
  • 打赏
  • 举报
回复
不会吧,认真点,A1下面还有好多子部门啊.
-狙击手- 2008-04-09
  • 打赏
  • 举报
回复
创建用户定义函数用于取每个父节点下子节点的信息
---

可创建一个函数来取得xx 部门为A1 的所有下属部门呀
zanyzyg 2008-04-09
  • 打赏
  • 举报
回复

没看明白!
yuanjun_xf 2008-04-09
  • 打赏
  • 举报
回复
select *
from ta
where corpid = 'xx'
and ( deptid ='A1' or parentid ='A1')
-狙击手- 2008-04-09
  • 打赏
  • 举报
回复
更正一下
corpid    deptid    parentid 

--------------
--创建用户定义函数用于取每个父节点下子节点的信息
create function f_getChild(@ID VARCHAR(10))
returns @t table(ID VARCHAR(10),PID VARCHAR(10),Level INT)
as
begin
declare @i int
set @i = 1
insert into @t select deptid,parentid,@i from ta where deptid= @ID

while @@rowcount<>0
begin
set @i = @i + 1

insert into @t
select
a.deptid,a.parentid,@i
from
BOM a,@t b
where
a.parentid=b.ID and b.Level = @i-1
end
return
end
go
-狙击手- 2008-04-09
  • 打赏
  • 举报
回复
corpid    deptid    parentid 

--------------
--创建用户定义函数用于取每个父节点下子节点的信息
create function f_getChild(@ID VARCHAR(10))
returns @t table(ID VARCHAR(10),PID VARCHAR(10),Level INT)
as
begin
declare @i int
set @i = 1
insert into @t select ID,PID,@i from ta where deptid= @ID

while @@rowcount<>0
begin
set @i = @i + 1

insert into @t
select
a.deptid,a.parentid,@i
from
BOM a,@t b
where
a.parentid=b.ID and b.Level = @i-1
end
return
end
go
isnewer 2008-04-09
  • 打赏
  • 举报
回复
没搞错啊,那A1下面的子部门出也取出来,虽然说不要把问题想复杂了,但也别把问题想简单了.
继续发言!
-狙击手- 2008-04-09
  • 打赏
  • 举报
回复

--or


select *
from ta
where (corpid = 'xx' and deptid = 'A1') or parentid = 'A1'
isnewer 2008-04-09
  • 打赏
  • 举报
回复
写错了,是把corpid='xx'的某一个部门(如'A1')的数据取出来.
不用写如何创建表,有表结构手工创建就可以了.

谢谢大家踊跃发言!
-狙击手- 2008-04-09
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 isnewer 的回复:]
写错了,是把corpid='xx'的某一个部门(如'A1')的数据取出来.
呵呵,是啊, 那也太简单了.
[/Quote]

select *
from ta
where corpid = 'xx' and deptid = 'A1'
isnewer 2008-04-09
  • 打赏
  • 举报
回复
写错了,是把corpid='xx'的某一个部门(如'A1')的数据取出来.
呵呵,是啊, 那也太简单了.
-狙击手- 2008-04-09
  • 打赏
  • 举报
回复
这个意思?

--生成测试数据
create table BOM(ID INT,PID INT,MSG VARCHAR(1000))
insert into BOM select 1,0,NULL
insert into BOM select 2,1,NULL
insert into BOM select 3,1,NULL
insert into BOM select 4,2,NULL
insert into BOM select 5,3,NULL
insert into BOM select 6,5,NULL
insert into BOM select 7,6,NULL
go

--创建用户定义函数用于取每个父节点下子节点的采购配置信息
create function f_getChild(@ID VARCHAR(10))
returns @t table(ID VARCHAR(10),PID VARCHAR(10),Level INT)
as
begin
declare @i int
set @i = 1
insert into @t select ID,PID,@i from BOM where PID = @ID

while @@rowcount<>0
begin
set @i = @i + 1

insert into @t
select
a.ID,a.PID,@i
from
BOM a,@t b
where
a.PID=b.ID and b.Level = @i-1
end
return
end
go

--执行查询
select ID from dbo.f_getChild(3)
go

--输出结果
/*
ID
----
5
6
7
*/

--删除测试数据
drop function f_getChild
drop table BOM

-狙击手- 2008-04-09
  • 打赏
  • 举报
回复
如何执行一条SQL语句把corpid为'xx'的所有部门都列出来?


--
select *
from ta
where corpid = 'xx'


子节点要不要?
tim_spac 2008-04-09
  • 打赏
  • 举报
回复
-- 如何执行一条SQL语句把corpid为'xx'的所有部门都列出来?
select deptid from [thetable] where corpid='xx'

可能这不是你要的输出效果,但内容应该没问题。
加载更多回复(2)

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧