环境Access,
表
CaseTypes案例分类,级别不限的那种,无限树那样的
CID int,
CPID int,
CName varchar(50)表CaseInfo案例信息
InfoID int,
InfoName varchar(50),
CID int,所属类别的ID,
问题,查询某个类别ID下的案例信息不好表达 了
就是案例信息里面的ID,只要他的父ID,爷ID,向上中包含这个ID,就查出来
不知道能不能有办法?路过SQL牛人的来看看,
表
CaseTypes案例分类,级别不限的那种,无限树那样的
CID int,
CPID int,
CName varchar(50)表CaseInfo案例信息
InfoID int,
InfoName varchar(50),
CID int,所属类别的ID,
问题,查询某个类别ID下的案例信息不好表达 了
就是案例信息里面的ID,只要他的父ID,爷ID,向上中包含这个ID,就查出来
不知道能不能有办法?路过SQL牛人的来看看,
解决方案 »
- 怎样查看数据库中所有索引
- 备份计划:每周做一次全备,每天间隔几个小时做一次日志备份,这样的备份计划合理么?
- sql语句中的exec用法
- 数据库并发加锁谁能给个例子啊?
- 求一sql分组方法??
- 求一SQL语句
- 怎样实现INTERNET上的前台和后台数据上传和下载(Access And Sql Server)!
- ??如何调用这样的存储过程(变量里有image OutPut)
- SQL Server中的存储过程sp_makewebtask中的参数是怎么用的,我看了它的bookonline但是还是不清楚?
- SQL-SERVER 2000 的服务突然停掉的问题? 谢谢大家.
- 请问使用数据库[发布和订阅] 能否同一台服务器上?
- SQL 2008上建的数据库如何最方便的还原至SQL2000上???
CREATE TABLE tb(ID char(3),PID char(3),Name nvarchar(10))
INSERT tb 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','小分市'
GO--查询指定节点及其所有子节点的函数
CREATE FUNCTION f_Cid(@ID char(3))
RETURNS @t_Level TABLE(ID char(3),Level int)
AS
BEGIN
DECLARE @Level int
SET @Level=1
INSERT @t_Level SELECT @ID,@Level
WHILE @@ROWCOUNT>0
BEGIN
SET @Level=@Level+1
INSERT @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--调用函数查询002及其所有子节点
SELECT a.*
FROM tb a,f_Cid('002') b
WHERE a.ID=b.ID
/*--结果
ID PID Name
------ ------- ----------
002 001 烟台市
004 002 招远市
CREATE TABLE tb(ID char(3),PID char(3),Name nvarchar(10))
INSERT tb 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','小分市'
GO--查询指定节点及其所有子节点的函数
CREATE FUNCTION f_Cid(@ID char(3))
RETURNS @t_Level TABLE(ID char(3),Level int)
AS
BEGIN
DECLARE @Level int
SET @Level=1
INSERT @t_Level SELECT @ID,@Level
WHILE @@ROWCOUNT>0
BEGIN
SET @Level=@Level+1
INSERT @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--调用函数查询002及其所有子节点
SELECT a.*
FROM tb a,f_Cid('002') b
WHERE a.ID=b.ID
/*--结果
ID PID Name
------ ------- ----------
002 001 烟台市
004 002 招远市
USE tempdb
GO-- 建立演示环境
CREATE TABLE Dept(
id int PRIMARY KEY,
parent_id int,
name nvarchar(20))
INSERT Dept
SELECT 0, 0, N'<全部>' UNION ALL
SELECT 1, 0, N'财务部' UNION ALL
SELECT 2, 0, N'行政部' UNION ALL
SELECT 3, 0, N'业务部' UNION ALL
SELECT 4, 0, N'业务部' UNION ALL
SELECT 5, 4, N'销售部' UNION ALL
SELECT 6, 4, N'MIS' UNION ALL
SELECT 7, 6, N'UI' UNION ALL
SELECT 8, 6, N'软件开发' UNION ALL
SELECT 9, 8, N'内部开发'
GO-- 查询指定部门下面的所有部门
DECLARE @Dept_name nvarchar(20)
SET @Dept_name = N'MIS'
;WITH
DEPTS AS(
-- 定位点成员
SELECT * FROM Dept
WHERE name = @Dept_name
UNION ALL
-- 递归成员, 通过引用CTE自身与Dept基表JOIN实现递归
SELECT A.*
FROM Dept A, DEPTS B
WHERE A.parent_id = B.id
)
SELECT * FROM DEPTS
GO-- 删除演示环境
DROP TABLE Dept
/*
(10 行受影响)
id parent_id name
----------- ----------- --------------------
6 4 MIS
7 6 UI
8 6 软件开发
9 8 内部开发(4 行受影响)
*/
CREATE TABLE BOM(PID INT,ID INT)
INSERT INTO BOM SELECT 801,101
INSERT INTO BOM SELECT 801,102
INSERT INTO BOM SELECT 801,103
INSERT INTO BOM SELECT 801,601
INSERT INTO BOM SELECT 601,101
INSERT INTO BOM SELECT 601,105
INSERT INTO BOM SELECT 601,501
INSERT INTO BOM SELECT 501,106
INSERT INTO BOM SELECT 501,121
GOCREATE FUNCTION F_GETROOT(@PID INT)
RETURNS INT
AS
BEGIN
DECLARE @ID INT
WHILE EXISTS(SELECT 1 FROM BOM WHERE ID=@PID)
BEGIN
SET @ID=@PID
SELECT @PID=PID FROM BOM WHERE ID=@ID
END
RETURN @PID
END
GOSELECT PID=DBO.F_GETROOT(PID),ID FROM BOM
GO/*
PID ID
----------- -----------
801 101
801 102
801 103
801 601
801 101
801 105
801 501
801 106
801 121
*/
DROP FUNCTION F_GETROOT
DROP TABLE BOM
GO
--生成测试数据
create table BOM_1(Item int,bom_head varchar(20),bom_child varchar(20),number int,products_attribute varchar(20))
insert into BOM_1 select 1 ,'A' ,'A1',1,'采购'
insert into BOM_1 select 2 ,'A' ,'A2',2,'生产'
insert into BOM_1 select 3 ,'A2','A3',3,'生产'
insert into BOM_1 select 4 ,'A2','A4',2,'采购'
insert into BOM_1 select 5 ,'A3','A5',2,'采购'
insert into BOM_1 select 6 ,'A3','A6',1,'采购'
insert into BOM_1 select 7 ,'B' ,'B1',1,'采购'
insert into BOM_1 select 8 ,'B' ,'B2',2,'生产'
insert into BOM_1 select 9 ,'B2','B3',3,'生产'
insert into BOM_1 select 10,'B2','B4',2,'采购'
insert into BOM_1 select 11,'B3','B5',2,'采购'
insert into BOM_1 select 12,'B3','B6',2,'采购'
go
--创建用户定义函数,用于取每个父节点下子节点的采购配置信息
create function f_stock(@bom_head varchar(20))
returns @t table(bom varchar(20),number int)
as
begin
declare @level int
declare @a table(bom varchar(20),number int,products_attribute varchar(20),[level] int)
set @level=1 if exists(select 1 from BOM_1 where bom_head=@bom_head)
insert into @a
select bom_child,number,products_attribute,@level
from BOM_1
where bom_head=@bom_head
while exists(select 1 from @a where [level]=@level and products_attribute='生产')
begin
set @level=@level+1
insert into @a(bom,number,products_attribute,[level])
select a.bom_child,a.number,a.products_attribute,@level
from BOM_1 a,@a b
where a.bom_head=b.bom and b.[level]=@level-1
end
insert into @t(bom,number) select bom,number from @a where products_attribute='采购'
return
end
go
--执行调用,取父节点'A'一个标准配置分解的采购信息及数量
select * from dbo.f_stock('A')
--生成测试数据
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创建用户定义函数,每个子节点de父节点的信息
--生成测试数据
create table BOM(ID int,parentID int,sClassName varchar(10))
insert into BOM values(1,0,'1111' )
insert into BOM values(2,1,'1111_1' )
insert into BOM values(3,2,'1111-1-1' )
insert into BOM values(4,3,'1111-1-1-1')
insert into BOM values(5,1,'1111-2' )go--创建用户定义函数,每个子节点de父节点的信息
create function f_getParent(@ID int)
returns varchar(40)
as
begin
declare @ret varchar(40) while exists(select 1 from BOM where ID=@ID and parentID<>0)
begin
select @ID=b.ID,@ret=','+rtrim(b.ID)+isnull(@ret,'')
from
BOM a,BOM b
where
a.ID=@ID and b.ID=a.parentID
end
set @ret=stuff(@ret,1,1,'')
return @ret
end
go--执行查询
select ID,isnull(dbo.f_getParent(ID),'') as parentID from BOM
go--输出结果
/*
ID parentID
----------- ----------------------------------------
1
2 1
3 1,2
4 1,2,3
5 1
*/--删除测试数据
drop function f_getParent
drop table BOM
go
--> 测试数据:@tb
create table tb([user_group_id] int,[per_group_id] int,[user_group_name] varchar(10))
insert tb
select 1,0,'总管理员' union all
select 2,1,'一级管理员' union all
select 3,2,'二级管理员' union all
select 4,3,'三级管理员' union all
select 8,4,'四级管理员' union all
select 10,8,'五级管理员'--2000
---创建函数
CREATE FUNCTION f_Cid(@name varchar(10))
RETURNS @t TABLE(ID varchar(10),Level int)
AS
BEGIN
DECLARE @Level int,@id int
SET @Level=1
set @id=(select [per_group_id] from tb where [user_group_name]=@name )
INSERT @t SELECT @ID,@Level
WHILE @@ROWCOUNT>0
BEGIN
SET @Level=@Level+1
INSERT @t SELECT a.[user_group_id],@Level
FROM tb a,@t b
WHERE a.[per_group_id]=b.id
AND b.Level=@Level-1
END
RETURN
END
GO--调用函数查询二级管理员及其所有子节点
SELECT a.*
FROM tb a,f_Cid('二级管理员') b
WHERE a.[per_group_id]=b.id/*
user_group_id per_group_id user_group_name
------------- ------------ ---------------
3 2 二级管理员
4 3 三级管理员
8 4 四级管理员
10 8 五级管理员(4 行受影响)
*/
--2005;with t as(
select * from tb where [user_group_name]='二级管理员'
union all
select a.* from tb a ,t where a.[per_group_id]=t.[user_group_id])
select * from t/*
user_group_id per_group_id user_group_name
------------- ------------ ---------------
3 2 二级管理员
4 3 三级管理员
8 4 四级管理员
10 8 五级管理员(4 行受影响)
*/
Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) Jul 9 2008 14:43:34 Copyright (c)
1988-2008 Microsoft Corporation Enterprise Evaluation Edition on Windows NT 5.1 <X86>
(Build 2600: Service Pack 3)
愿和大家共同进步
如有雷同、实属巧合
●●●●●2009-09-03 17:47:36.077●●●●●
★★★★★soft_wsx★★★★★
*/
--树型结构处理之双编号(广度深度排序)
if OBJECTPROPERTY(object_id('tb'),'isusertable')<>0
drop table tb
create table tb(ybh nvarchar(10),ebh nvarchar(10),beizhu nvarchar(1000))
insert tb
select '0001',null,'云南省'
union all select '0002','0001','昆明市'
union all select '0003','0001','昭通市'
union all select '0009','0001','大理市'
union all select '0008',null,'四川省'
union all select '0004',null,'贵州省'
union all select '0005','0002','五华区'
union all select '0007','0002','水富县'
union all select '0006','0005','西园路192号'
union all select '0010','0006','金色梧桐'
union all select '0011','0010','科技有限公司'
union all select '0015','0007','两碗乡'
union all select '0013','0015','两碗村'
union all select '0012','0013','某跨国集团董事长'
union all select '0014','0008','成都市'--select * from tb
--广度排序(先显示第一层节点,再显示第二次节点......)
--定义辅助表
declare @level_tb table(bh nvarchar(10),level int)
declare @level int
set @level=0
insert @level_tb(bh,level)
select ybh,@level from tb where ebh is null
while @@ROWCOUNT>0
begin
set @level=@level+1
insert @level_tb(bh,level)
select ybh,@level
from tb a,@level_tb b
where a.ebh=b.bh
and b.level=@level-1
end
select a.*,b.* from tb a,@level_tb b where a.ybh=b.bh order by level
/*
ybh ebh beizhu bh level
0001 NULL 云南省 0001 0
0008 NULL 四川省 0008 0
0004 NULL 贵州省 0004 0
0002 0001 昆明市 0002 1
0003 0001 昭通市 0003 1
0009 0001 大理市 0009 1
0014 0008 成都市 0014 1
0005 0002 五华区 0005 2
0007 0002 水富县 0007 2
0006 0005 西园路192号 0006 3
0015 0007 两碗乡 0015 3
0010 0006 金色梧桐 0010 4
0013 0015 两碗村 0013 4
0011 0010 科技有限公司 0011 5
0012 0013 某跨国集团董事长 0012 5
*/
--深度排序(模拟单编码法)
declare @level_tt table(ybh nvarchar(1000),ebh nvarchar(1000),level int)
declare @level int
set @level=0
insert @level_tt(ybh,ebh,level)
select ybh,ybh,@level from tb where ebh is null
while @@ROWCOUNT>0
begin
set @level=@level+1
insert @level_tt(ybh,ebh,level)
select a.ybh,b.ebh+a.ybh,@level
from tb a,@level_tt b
where a.ebh=b.ybh and b.level=@level-1
end
select space(b.level*2)+'----'+a.beizhu,a.*,b.*
from tb a,@level_tt b
where a.ybh=b.ybh
order by b.ebh
/*(无列名) ybh ebh beizhu ybh ebh level
----云南省 0001 NULL 云南省 0001 0001 0
----昆明市 0002 0001 昆明市 0002 00010002 1
----五华区 0005 0002 五华区 0005 000100020005 2
----西园路192号 0006 0005 西园路192号 0006 0001000200050006 3
----金色梧桐 0010 0006 金色梧桐 0010 00010002000500060010 4
----科技有限公司 0011 0010 科技有限公司 0011 000100020005000600100011 5
----水富县 0007 0002 水富县 0007 000100020007 2
----两碗乡 0015 0007 两碗乡 0015 0001000200070015 3
----两碗村 0013 0015 两碗村 0013 00010002000700150013 4
----某跨国集团董事长 0012 0013 某跨国集团董事长 0012 000100020007001500130012 5
----昭通市 0003 0001 昭通市 0003 00010003 1
----大理市 0009 0001 大理市 0009 00010009 1
----贵州省 0004 NULL 贵州省 0004 0004 0
----四川省 0008 NULL 四川省 0008 0008 0
----成都市 0014 0008 成都市 0014 00080014 1
*/
--查找子节点(包括本身节点和子节点)
declare @level_tt table(ybh nvarchar(1000),ebh nvarchar(1000),level int)
declare @level int
set @level=0
insert @level_tt(ybh,ebh,level)
select ybh,ybh,@level from tb where ybh='0005'
while @@ROWCOUNT>0
begin
set @level=@level+1
insert @level_tt(ybh,ebh,level)
select a.ybh,b.ebh+a.ybh,@level
from tb a,@level_tt b
where a.ebh=b.ybh and b.level=@level-1
end
select space(b.level*2)+'----'+a.beizhu,a.*,b.*
from tb a,@level_tt b
where a.ybh=b.ybh
order by b.ebh
/*
(无列名) ybh ebh beizhu ybh ebh level
----五华区 0005 0002 五华区 0005 0005 0
----西园路192号 0006 0005 西园路192号 0006 00050006 1
----金色梧桐 0010 0006 金色梧桐 0010 000500060010 2
----科技有限公司 0011 0010 科技有限公司 0011 0005000600100011 3
*/
--查的父节点(包括本身节点和所有的你节点)
declare @level_tt table(ybh nvarchar(1000),ebh nvarchar(1000),level int)
declare @level int
set @level=0
insert @level_tt(ybh,ebh,level)
select ybh,ebh,@level from tb where ebh='0005'
while @@ROWCOUNT>0
begin
set @level=@level+1
insert @level_tt(ybh,ebh,level)
select a.ebh,b.ebh+a.ebh,@level
from tb a,@level_tt b
where a.ybh=b.ybh and b.level=@level-1
end
select space(b.level*2)+'----'+a.beizhu,a.*,b.*
from tb a,@level_tt b
where a.ybh=b.ybh
order by b.ebh desc
/*
(无列名) ybh ebh beizhu ybh ebh level
----云南省 0001 NULL 云南省 0001 0005000500020001 3
----昆明市 0002 0001 昆明市 0002 000500050002 2
----五华区 0005 0002 五华区 0005 00050005 1
----西园路192号 0006 0005 西园路192号 0006 0005 0
*/
Access可以用函数么?
哎,开始高兴的看到这么多答案,可是回来一看却不能用,伤心啊,,,
不过还是非常感谢,继续等人解答,,我看了下Access好像不支持自定义函数,,现在有个想法就是先根据ID却查ID和PID然后取出来用c#递归,看哪些符合要求的,再到Access里去查询,,,可是还是不明白能怎么用c#去递归呢?
ACCESS,不支持用户定义函数和存储过程, 不过它是基于VBA的,支持VBS的函数,这很有诱惑哦。数据量不大, 你还是在程序里面用函数递归检索来实现你要的结果吧, sql语句没法实现, 俺早年弄网站bbs论坛树型回帖的时候就一个sub递归搞定。如果可以更改数据结构的话, 我可以给你个思路, 大致如下:表CaseTypes加一个字段, 保存每个节点的路径(path)CID int,
CPID int,
CName varchar(50),
CPath Memo (用来保存对应的父节点的路径,(CPath + '_' + CID + '_')标识该结点的路径。 该字段最大容量是2G, 你的级别不会深到把它撑破吧)用下面数据来说明如何操作
CID CName CPID CPath
1 Node01 0 _0_
2 Node02 1 _0_1_
3 Node03 2 _0_1_2_
4 Node04 3 _0_1_2_3_Node01
------Node02
------Node03
------Node03
查找节点ID是1的所有子节点:
SELECT * from CaseTypes where instr(cstr(CPath),'_1_')>0 ;
CID CName CPID CPath
2 Node02 1 _0_1_
3 Node03 2 _0_1_2_
4 Node04 3 _0_1_2_3_
查找节点ID是2的所有子节点:
SELECT * from CaseTypes where instr(cstr(CPath),'_2_')>0 ;
CID CName CPID CPath
3 Node03 2 _0_1_2_
4 Node04 3 _0_1_2_3_
查找节点ID是3的所有父节点:
SELECT * from CaseTypes where CID<>3 and instr('_0_1_2_',cstr(CPath))=1 ;
CID CName CPID CPath
1 Node01 0 _0_
2 Node02 1 _0_1_
是SQL问题啊,所以就来这里了,,,你说的这个不符合要求,数据是已经存在了,我不可能给每个记录加一列,,
waiting................
自己解决,结帖,非常感谢LS提供的MS-Sqlserver中 方法