Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Recursive table traversal via Recordset

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-08-03 : 09:59:58
Greg writes "


const REC_ID = 0
const PARENT_REC_ID = 1
const REC_TITLE = 2
const REC_NUMBER = 3

dim cn, rstPhone, strSQL
dim strArrRows, intRowCount, intIndex


strSQL = "SELECT numID, numParentID, txtTitle, txtNumber " & _
"FROM tblPhoneList " & _
"ORDER BY numID "


' function to get connection, in another include
set cn = fncGetConnection( )

set rstPhone = server.CreateObject( "ADODB.Recordset" )

rstPhone.ActiveConnection = cn
rstPhone.CursorType = adOpenStatic
rstPhone.Open strSQL

strArrRows = rstPhone.GetRows()
intRowCount = rstPhone.RecordCount
rstPhone.Close
cn.Close
set rstPhone = nothing
set cn = nothing

intIndex=0

-- Some HTML stuff

' Find the top-level entities in the hierarchy
for intIndex = 0 To intRowCount - 1
'Response.Write "intIndex = " & intIndex
' If the Parent is equal 0(is NULL), we have a top level
if int(strArrRows(PARENT_REC_ID, intIndex)) = 0 then
' Start looking for children
PrintRow strArrRows(REC_ID, intIndex), strArrRows(REC_TITLE, intIndex), strArrRows( REC_NUMBER, intIndex ), 0
end if
next

sub PrintRow(nRegionID, strTitle, strNumber, intLevel )
dim intIndex

'Write out the current level
Response.Write "<TR>" & Cat( intLevel, "<TD></TD>") & "<TD>" & strTitle & "</TD><TD>" & strNumber & "</TD></TR>"
for intIndex = 0 To intRowCount - 1
' IF the row we're looking at has a parent of the region passed in, then delve into its children.
if strArrRows(PARENT_REC_ID, intIndex) = nRegionID then
' Call our PrintRow function to find the children of this record and so on.
PrintRow strArrRows(REC_ID, intIndex), strArrRows(REC_TITLE, intIndex), strArrRows( REC_NUMBER, intIndex ), intLevel + 1
end if
Next
end sub

function Cat( intTimes, strWhat )
dim i, strTemp
for i = 1 to intTimes
strTemp = strTemp & strWhat
next
Cat = strTemp
end function



This might help someone, I found the outline somewhere else on the web.

Doing a search is a challenge though, just finding where you are starting from... I guess it will require finding all of the head parents within the set of data."
   

- Advertisement -