--sql 2000中的函数解决办法./*
标题:查询指定节点及其所有子节点的函数
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-05-12
地点:广东深圳
*/create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
insert into tb values('001' , null , '广东省')
insert into tb values('002' , '001' , '广州市')
insert into tb values('003' , '001' , '深圳市')
insert into tb values('004' , '002' , '天河区')
insert into tb values('005' , '003' , '罗湖区')
insert into tb values('006' , '003' , '福田区')
insert into tb values('007' , '003' , '宝安区')
insert into tb values('008' , '007' , '西乡镇')
insert into tb values('009' , '007' , '龙华镇')
insert into tb values('010' , '007' , '松岗镇')
go--查询指定节点及其所有子节点的函数
create function f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int)
as
begin
declare @level int
set @level = 1
insert into @t_level select @id , @level
while @@ROWCOUNT > 0
begin
set @level = @level + 1
insert into @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--调用函数查询001(广东省)及其所有子节点
select a.* from tb a , f_cid('001') b where a.id = b.id order by a.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 行)
*/--调用函数查询002(广州市)及其所有子节点
select a.* from tb a , f_cid('002') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
002 001 广州市
004 002 天河区(所影响的行数为 2 行)
*/--调用函数查询003(深圳市)及其所有子节点
select a.* from tb a , f_cid('003') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
003 001 深圳市
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇(所影响的行数为 7 行)
*/drop table tb
drop function f_cid/*
标题:查询指定节点及其所有父节点的函数
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-05-12
地点:广东深圳
*/create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
insert into tb values('001' , null , '广东省')
insert into tb values('002' , '001' , '广州市')
insert into tb values('003' , '001' , '深圳市')
insert into tb values('004' , '002' , '天河区')
insert into tb values('005' , '003' , '罗湖区')
insert into tb values('006' , '003' , '福田区')
insert into tb values('007' , '003' , '宝安区')
insert into tb values('008' , '007' , '西乡镇')
insert into tb values('009' , '007' , '龙华镇')
insert into tb values('010' , '007' , '松岗镇')
go--查询指定节点及其所有父节点的函数
create function f_pid(@id varchar(3)) returns @t_level table(id varchar(3))
as
begin
insert into @t_level select @id
select @id = pid from tb where id = @id and pid is not null
while @@ROWCOUNT > 0
begin
insert into @t_level select @id select @id = pid from tb where id = @id and pid is not null
end
return
end
go--调用函数查询002(广州市)及其所有父节点
select a.* from tb a , f_pid('002') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
001 NULL 广东省
002 001 广州市(所影响的行数为 2 行)
*/--调用函数查询003(深圳市)及其所有父节点
select a.* from tb a , f_pid('003') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
001 NULL 广东省
003 001 深圳市(所影响的行数为 2 行)
*/--调用函数查询008(西乡镇)及其所有父节点
select a.* from tb a , f_pid('008') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
001 NULL 广东省
003 001 深圳市
007 003 宝安区
008 007 西乡镇(所影响的行数为 4 行)
*/drop table tb
drop function f_pid
标题:查询指定节点及其所有子节点的函数
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-05-12
地点:广东深圳
*/create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
insert into tb values('001' , null , '广东省')
insert into tb values('002' , '001' , '广州市')
insert into tb values('003' , '001' , '深圳市')
insert into tb values('004' , '002' , '天河区')
insert into tb values('005' , '003' , '罗湖区')
insert into tb values('006' , '003' , '福田区')
insert into tb values('007' , '003' , '宝安区')
insert into tb values('008' , '007' , '西乡镇')
insert into tb values('009' , '007' , '龙华镇')
insert into tb values('010' , '007' , '松岗镇')
go--查询指定节点及其所有子节点的函数
create function f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int)
as
begin
declare @level int
set @level = 1
insert into @t_level select @id , @level
while @@ROWCOUNT > 0
begin
set @level = @level + 1
insert into @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--调用函数查询001(广东省)及其所有子节点
select a.* from tb a , f_cid('001') b where a.id = b.id order by a.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 行)
*/--调用函数查询002(广州市)及其所有子节点
select a.* from tb a , f_cid('002') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
002 001 广州市
004 002 天河区(所影响的行数为 2 行)
*/--调用函数查询003(深圳市)及其所有子节点
select a.* from tb a , f_cid('003') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
003 001 深圳市
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇(所影响的行数为 7 行)
*/drop table tb
drop function f_cid/*
标题:查询指定节点及其所有父节点的函数
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-05-12
地点:广东深圳
*/create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
insert into tb values('001' , null , '广东省')
insert into tb values('002' , '001' , '广州市')
insert into tb values('003' , '001' , '深圳市')
insert into tb values('004' , '002' , '天河区')
insert into tb values('005' , '003' , '罗湖区')
insert into tb values('006' , '003' , '福田区')
insert into tb values('007' , '003' , '宝安区')
insert into tb values('008' , '007' , '西乡镇')
insert into tb values('009' , '007' , '龙华镇')
insert into tb values('010' , '007' , '松岗镇')
go--查询指定节点及其所有父节点的函数
create function f_pid(@id varchar(3)) returns @t_level table(id varchar(3))
as
begin
insert into @t_level select @id
select @id = pid from tb where id = @id and pid is not null
while @@ROWCOUNT > 0
begin
insert into @t_level select @id select @id = pid from tb where id = @id and pid is not null
end
return
end
go--调用函数查询002(广州市)及其所有父节点
select a.* from tb a , f_pid('002') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
001 NULL 广东省
002 001 广州市(所影响的行数为 2 行)
*/--调用函数查询003(深圳市)及其所有父节点
select a.* from tb a , f_pid('003') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
001 NULL 广东省
003 001 深圳市(所影响的行数为 2 行)
*/--调用函数查询008(西乡镇)及其所有父节点
select a.* from tb a , f_pid('008') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
001 NULL 广东省
003 001 深圳市
007 003 宝安区
008 007 西乡镇(所影响的行数为 4 行)
*/drop table tb
drop function f_pid
RETURNS TABLE
AS
RETURN(
WITH ptb([id],[level])
AS(
SELECT [pid],1 FROM [tb]
WHERE [id]=@id
AND [pid] <>0
UNION ALL
SELECT A.[pid],B.[level]+1
FROM [tb] A,ptb B
WHERE A.[id]=B.[id]
AND [pid] <>0)
SELECT * FROM ptb
)
GO
--调用(查询所有的父)
SELECT A.*,层次=B.[level]
FROM [tb] A,[f_pid](7)B
WHERE A.[id]=B.[id]
GO
DECLARE @T TABLE (BOM_NO INT,PRD_NO INT,NAME VARCHAR(5))
INSERT INTO @T
SELECT 123456,1234561,'测试1' UNION ALL
SELECT 123456,1234562,'测试2' UNION ALL
SELECT 1234561,1234563,'测试3' UNION ALL
SELECT 1234561,1234564,'测试4' UNION ALL
SELECT 123458,1234581,'测试5' UNION ALL
SELECT 123459,1234591,'测试6' UNION ALL
SELECT 123459,1234592,'测试7' UNION ALL
SELECT 1234564,12345641,'测试8'--SQL查询如下:DECLARE @BOM_NO INT
SET @BOM_NO=123456;WITH Liang AS
(
SELECT
BOM_NO,PRD_NO,NAME,PATH=CAST(NAME AS VARCHAR(MAX))
FROM @T
WHERE BOM_NO=@BOM_NO
UNION ALL
SELECT
A.BOM_NO,
A.PRD_NO,
A.NAME,
B.PATH+'->'+A.NAME
FROM @T AS A
JOIN Liang AS B
ON A.BOM_NO=B.PRD_NO
)
SELECT *
FROM Liang/*
BOM_NO PRD_NO NAME PATH
----------- ----------- ----- ---------------------------------------
123456 1234561 测试1 测试1
123456 1234562 测试2 测试2
1234561 1234563 测试3 测试1->测试3
1234561 1234564 测试4 测试1->测试4
1234564 12345641 测试8 测试1->测试4->测试8 (5 行受影响)
*/
我用的是sql 2000:)
功能:返回部门的最上级一级部门ID
*/
CREATE Function DBO.GetAdminDepIDEx1(@DepID varchar(10))
Returns varchar(10)
AS
Begin
Declare @AdminID varchar(10),@DepGrade varchar(6)
DECLARE @BLoop int
set @BLoop = 0
WHILE @BLoop = 0
BEGIN
if(not exists (Select 1 From Department Where DepID=@DepID))
begin
set @BLoop =1
Set @AdminID=@DepID
BREAK
end
Select @AdminID = AdminID,@DepGrade=DepGrade From Department Where DepID=@DepID
if @DepGrade<>'G01' and ISNULL(@DepGrade,'')<>''
SET @DepID = @AdminID
ELSE
BEGIN
set @BLoop =1
Set @AdminID=@DepID
BREAK
END
END
return @AdminID
END我写成如上函数查询如下语句是可以:
select DBO.GetAdminDepIDEx1('D206')但是如果我执行如下查询,则查询分析器当机:
select DepID,DBO.GetAdminDepIDEx1(DepID) TopDepID
From Department
select DepID,DBO.GetAdminDepIDEx1(DepID) TopDepID
From Department --
你要注意你的最顶层是不是死循环咯
select DepID,DBO.GetAdminDepIDEx1(DepID) TopDepID
From Department --
你要注意你的最顶层是不是死循环咯
/*
标题:查询指定节点的最上级父节点的函数
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2009-01-21
地点:新疆乌鲁木齐
*/create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
insert into tb values('001' , null , '广东省')
insert into tb values('002' , '001' , '广州市')
insert into tb values('003' , '001' , '深圳市')
insert into tb values('004' , '002' , '天河区')
insert into tb values('005' , '003' , '罗湖区')
insert into tb values('006' , '003' , '福田区')
insert into tb values('007' , '003' , '宝安区')
insert into tb values('008' , '007' , '西乡镇')
insert into tb values('009' , '007' , '龙华镇')
insert into tb values('010' , '007' , '松岗镇')
go--查询指定节点最上级父节点的函数
create function f_pid(@id varchar(3)) returns @t_level table(id varchar(3))
as
begin
insert into @t_level select @id
select @id = pid from tb where id = @id and pid is not null
while @@ROWCOUNT > 0
begin
delete from @t_level
insert into @t_level select @id select @id = pid from tb where id = @id and pid is not null
end
return
end
go--调用函数查询005(罗湖区)的最上级父节点
select a.* from tb a , f_pid('005') b where a.id = b.id
/*
id pid name
---- ---- ----------
001 NULL 广东省(所影响的行数为 1 行)
*/--调用函数查询002(广州市)的最上级父节点
select a.* from tb a , f_pid('002') b where a.id = b.id
/*
id pid name
---- ---- ----------
001 NULL 广东省(所影响的行数为 1 行)
*/--调用函数查询001(广东省)的最上级父节点
select a.* from tb a , f_pid('001') b where a.id = b.id
/*
id pid name
---- ---- ----------
001 NULL 广东省(所影响的行数为 1 行)
*/drop table tb
drop function f_pid
Returns varchar(10)
AS
Begin
Declare @DepGrade varchar(6)
Select @DepID = AdminID,@DepGrade=DepGrade
From Department Where DepID=@DepID and DepID<>AdminID and AdminID is not null WHILE @@ROWCOUNT > 0
BEGIN
IF @DepGrade<>'G01' and @DepGrade is not null
Select @DepID = AdminID,@DepGrade=DepGrade
From Department Where DepID=@DepID and DepID<>AdminID and AdminID is not null
ELSE
BREAK
END
return @DepID
END解决方案:修改函数如上
问题原因:部门代码和上级部门代码一致,导致出现死循环