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 2005 Forums
 Transact-SQL (2005)
 Trying to avoid using a cursor

Author  Topic 

madrak
Starting Member

20 Posts

Posted - 2007-03-07 : 15:57:03
Say that we have the following setup:

Root Object
|-SubObjectA
| |-SubObjectA1
| |-SubObjectA2
|-SubObjectB

with a table definition like this:
ObjectID | ObjectName | ParentObjectID
1 SubObjectA 1
2 SubObjectB 1
3 SubObjectA1 2
4 SubObjectA2 2

What I would like to do, is pass back to the calling application a table that looks like this:

PK | ObjectID | ObjectName | ObjectDepth
1 1 Object A 1
2 3 Object A1 2
3 4 Object A2 2
4 2 Object B 1

I am currently doing this using recursion, but am failing miserably. I talked to another guy here at work and he mentioned the possibility of using a Cursor, but that they can add alot of overhead, so I would like to avoid that if possible.

Is there a way to do this other than with a cursor?

Any suggestions would be greatly appreciated

-madrak

X002548
Not Just a Number

15586 Posts

Posted - 2007-03-07 : 16:46:55
what version of sql server?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

madrak
Starting Member

20 Posts

Posted - 2007-03-07 : 16:59:49
SQL Server 2000
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2007-03-07 : 17:09:29
too bad, 2k5 has recursive queries

However....(and I thought I posted this)

What is the format of the result set you want

You could use a series of work tables if you know how deep the tree is...or dynamic sql....it all depends on the final result

Look here

http://weblogs.sqlteam.com/brettk/archive/2006/11/13/23995.aspx



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2007-03-07 : 17:17:49
quote:
Originally posted by madrak

SQL Server 2000


This is a 2005 forum, it's important that you post in the right forum, because you'll get a solution here that isn't going to work for you because it will use 2005 features (in this case a recursive CTE will do what you want, but that's not available on 2000).
Go to Top of Page

madrak
Starting Member

20 Posts

Posted - 2007-03-07 : 17:25:01
Whoops, sorry, you're right. I'll repost in the T-SQL2000 forums, sorry about that
Go to Top of Page

madrak
Starting Member

20 Posts

Posted - 2007-03-07 : 17:30:25
quote:
Originally posted by X002548


You could use a series of work tables if you know how deep the tree is...or dynamic sql....it all depends on the final result

Look here

http://weblogs.sqlteam.com/brettk/archive/2006/11/13/23995.aspx





The final results will look like the PK | ObjectID | ObjectName | Object Depth table posted above

i don't know the depth of the possible table ahead of time, so it has to be entirely dynamic, which sounds like the work tables won't work, but dynamic sql might, if you could point me in the direction of some resources for that I would greatly appreciate it, I will, of course, also google for it
Go to Top of Page

snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2007-03-07 : 17:48:27
Continued at http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=80220
Go to Top of Page
   

- Advertisement -