PHP中的一个遍历问题网状结构(非源结点)转到SQL存储过程
--PHP代码(遍历网状结构中的问题节点(排除源结点))
/**
* 获得相同问题的子问题(包含本源问题)
* @param Object $handle
* @param array $arr
* @param string $eid
*/
function getChilds($handle,&$arr,$eid) {
$sql="select EID2 from ERelation where EID1='".$eid."' and RelationID='3'";
$result = $handle->gets($sql);
if ($result) {
for ($i = 0; $i < count($result); $i++) {
$arr[]=$result[$i]['EID2'];
getChilds($handle, $arr,$result[$i]['EID2']);
}
}
}
//select EID1 from ERelation where EID2='$eid' and RelationID='3'
/**
* 获得相同问题的父问题(包含本源问题)
* @param Object $handle
* @param array $arr
* @param string $eid
*/
function getParents($handle,&$arr,$eid) {
$sql2="select EID1 from ERelation where EID2='$eid' and RelationID='3'";
$result2=$handle->gets($sql2);
if ($result2) {
for ($i = 0; $i < count($result2); $i++) {
$arr[]=$result2[$i]['EID1'];
getChilds($handle, $arr, $result2[$i]['EID1']);
getParents($handle, $arr, $result2[$i]['EID1']);
}
}
}
--PHP转SQL存储过程
create procedure proc_getChilds(@EIDParent varchar(50))
as
begin
declare @EID2 varchar(50)
declare ERelationChild_Cursor cursor local for
select EID2 from ERelation where EID1=@EIDParent and RelationID='3';
open ERelationChild_Cursor
FETCH NEXT FROM ERelationChild_Cursor into @EID2
if len(@EID2)>0
begin
insert into QTemp(EID) values(@EID2);
FETCH NEXT FROM ERelationChild_Cursor into @EID2;
exec proc_getChilds @EID2;
end
close ERelationChild_Cursor;
deallocate ERelationChild_Cursor;
end
go
create procedure proc_getParents(@EIDSon varchar(50))
as
begin
declare @EID1 varchar(50)
declare ERelationParent_Cursor cursor local for
select EID1 from ERelation where EID2=@EIDSon and RelationID='3';
open ERelationParent_Cursor
FETCH NEXT FROM ERelationParent_Cursor into @EID1;
if len(@EID1)>0
begin
insert into QTemp(EID) values(@EID1);
FETCH NEXT FROM ERelationParent_Cursor into @EID1;
exec proc_getChilds @EID1;
exec proc_getParents @EID1;
end
close ERelationParent_Cursor;
deallocate ERelationParent_Cursor;
end
go
---------------
declare @EID varchar(50)
set @EID='9FA90EBC-38D4-233E-BDE5-C8D63923462E'
exec proc_getChilds @EID
exec proc_getParents @EID
select * from QTemp