哈 先谢谢各位的帮忙 祝中秋happy
问题:
Data_Merch(父子档案关系表)
FMerch SMerch
b c
c d
d a
=============
这是表中数据
现在要插入数据
比如(x,y)
插入前想判断是否出现死循环
也就是y的所有子档案里面(无限层),如果含有x,就是错误,不能插入
还有x的徐哦有父档案里面(无限层),如果含有y,就有错误,不能插入比如 插入 (a,b) 这个是错误的 因为 b->c->d->a 形成了死循环
插入(a,d),也是错误的不能准确表达,就这样描述一下
请问怎么实现比较好
用SQL语句如不行,借助代码 谢谢先~
问题:
Data_Merch(父子档案关系表)
FMerch SMerch
b c
c d
d a
=============
这是表中数据
现在要插入数据
比如(x,y)
插入前想判断是否出现死循环
也就是y的所有子档案里面(无限层),如果含有x,就是错误,不能插入
还有x的徐哦有父档案里面(无限层),如果含有y,就有错误,不能插入比如 插入 (a,b) 这个是错误的 因为 b->c->d->a 形成了死循环
插入(a,d),也是错误的不能准确表达,就这样描述一下
请问怎么实现比较好
用SQL语句如不行,借助代码 谢谢先~
解决方案 »
- 新安装的sqlserver2005没有数据库,也没有实例
- 触发器问题(各位帮帮忙)!!
- 这段SQL报错﹐如何改,请帮忙?
- 将Access 2000的数据导入SQL Server 2000,可能会出现哪些错误?
- 如何得到SQL Server实例的当前连接数?指实际连接的客户端个数。
- 可以将sqlserver个人版升级为企业版?
- 存储过程导致timeout问题!
- 关于sql server存储过程用image做参数,通不过?请看代码!
- pb6.5连接ase12,打开表时,出错,如下:
- 小弟明天要去面试.帮帮忙!!
- ****将查询结果导出到Excel问题****
- asp中的NULL和sql中的NULL是不是一个NULL?
FMerch SMerch
b c
c d
d e
e f
e g
d a
==============
我要得到结果
b c
b d
b e
b f
b g
b a
set @i=1
while @i<=100
begin
insert into #Temp select * from #T where F = 'a'
insert into #Temp select * from #T where F in (select Z from #Temp)
set @i = @i + 1
endselect distinct z,* from #Temp我是把循环次数写死
是在没辙
---------
FMerch SMerch Layer
0 b 1
b c 2
c d 3
d a 4
CREATE PROCEDURE [getP]
@Son nvarchar(50),
@SourceParent nvarchar(50)
AS
DECLARE @parent nvarchar(50)
DECLARE @return_status intDECLARE testcursor CURSOR FOR
select FMerch
from Data_Merch
where SMerch = @SonOPEN testcursor FETCH NEXT FROM testcursor
into @parent
WHILE @@FETCH_STATUS = 0
BEGIN
if @parent = @SourceParent
begin
return -1
end
exec @return_status = getP @parent
if @return_status = -1
begin
return -1
end
FETCH NEXT FROM testcursor
into @parent
ENDCLOSE testcursor
DEALLOCATE testcursor
return 0
比如 插入 (a,b)前
exec @return_status = getP 'b','a'
if @return_status = -1
begin
--父节点有重复
end
注:
上一贴存储过程中有笔误
exec @return_status = getP @parent改为
exec @return_status = getP @parent,@SourceParent
Data_Merch(父子档案关系表)
FMerch SMerch
b c
c d
d e
e f
e g
d a
==============
我要得到结果
b c
b d
b e
b f
b g
b a
declare @P nvarchar(50)
declare @S nvarchar(50)
set @P = 'a'
set @S = 'b
'
declare @i int
declare @J int
create #T
{
a vchar(50),
}insert #T value(@P)
set @i =1
while @i <> 0
begin
select FMerch into #T
from Data_Merch where SMerch in (select a from #T)
select @i = count(*) from #T
select @J = count(*) from #T where a = @S
if @J<> 0
begin
return -1 --有重复的FMerch
end
endinsert #T value(@S)
set @i =1
while @i <> 0
begin
select SMerch into #T
from Data_Merch where FMerch in (select a from #T)
select @i = count(*) from #T
select @J = count(*) from #T where a = @P
if @J<> 0
begin
return -2 --有重复的SMerch
end
end
Data_Merch(父子档案关系表)
FMerch SMerch
b c
c d
d e
e f
e g
d a
==============
我要得到结果
b c
b d
b e
b f
b g
b a-----临时表的写法
declare @P nvarchar(50)
set @P = 'b'
'
declare @i int
declare @J int
create #T
{
P vchar(50),
S vchar(50),
Layer int
}
set @J = 1
select SMerch as SMerch into #T1 from Data_Merch where FMerch = @P
select @i = count(*) from #T1
insert into #T(s,P,Layer) select SMerch ,@P,@J from #T1
while @i <> 0
begin
set @J = @J + 1
select SMerch as SMerch into #T1 from Data_Merch where FMerch in (select SMerch from #t1)
select @i = count(*) from #T1
insert into #T(s,P,Layer) select SMerch ,@P,@J from #T1
end
select * from #T