Create Table emp
(
IDint,
parentID int
)
insert into emp values(1 , 0)
insert into emp values(5 , 3)
insert into emp values(3 , 1)
insert into emp values(4 , 2)
insert into emp values(2 , 1)
--按id拼个路径出来,然后按路径排序即可
--这是mssql的写法,db2我不知道
--search BREADTH first by ID set sort,估计是按用来产生排序的
With Report(ParentID,ID,Level)
As
(
Select parentID,ID,Convert(Varbinary(Max),id)
From emp Where parentID='0'
Union All
Select empparentID,empID,Level+Convert(Varbinary,empid)
From report Join emp
On empparentID=reportID
)
Select ID,parentID From report
Order By Level
--或者
With Report(ParentID,ID,Level)
As
(
Select parentID,ID,Convert(Varchar(Max),Right('00000'+id,5))
From emp Where parentID='0'
Union All
Select empparentID,empID,Level+'-'+Convert(Varchar(Max),Right('00000'+empid,5))
From report Join emp
On empparentID=reportID
)
Select ID,parentID From report
Order By Level
ORACLE 有取下一条分组记录的分析函数。
SELECT COL1,COL2, COL3 FROM
(SELECT COL1, COL2, LEAD(COL1) OVER(ORDER BY COL1) AS COL3 FROM TEST12)
WHERE COL2 > COL3
或者
SELECT ACOL1,ACOL2,
(SELECT COL1 FROM (SELECT COL1, ROWNUM NUM FROM TEST12 ORDER BY COL1) WHERE NUM = ANUM + 1) AS COL3
FROM
(SELECT COL1, COL2, ROWNUM AS NUM FROM TEST12 ORDER BY COL1) A
do
{
while (odrRead())
{
string strYxmc=odr[0]ToString();
ResponseWrite(strYxmc);
}
}while(odrNextResult());
看起来是这句rsgetString("orderID")出错,
前面加上这句试试:
rsnext()
多条语句的话,可以这样用:
while(rsnext())
{
job = rsgetString("orderID");
}
以上就是关于db2数据库sql语句遍历一张父子节点相关联的表,运用CTE算法全部的内容,包括:db2数据库sql语句遍历一张父子节点相关联的表,运用CTE算法、一条sql语句怎么写,遍历数据库进行比较。。高手指点啊、C# 如何遍历数据库等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)