Oh hell, that's hideously simple...

CREATE PROCEDURE dbo.sp_organizations
@parent_id int
AS
DECLARE @Name varchar(500)
DECLARE @Parent int

DECLARE cur_Level CURSOR LOCAL FOR
SELECT Organization_id AS ID,
  Organization_nm, Parent_organization_id
FROM  Organization
WHERE Parent_organization_id = @parent_id
ORDER BY Organization_nm

CREATE TABLE #child (id int, name nvarchar(500), parent int)

OPEN cur_Level
        FETCH NEXT FROM cur_Level INTO @parent_id, @Name, @Parent
        WHILE @@FETCH_STATUS = 0
        BEGIN
                INSERT INTO #child (id, name, parent)
                VALUES (@parent_id, @name, @parent)

                EXEC sp_organizations @parent_id
                FETCH NEXT FROM cur_Level INTO @parent_id, @Name,
                @Parent
        END
CLOSE cur_Level
DEALLOCATE cur_Level

SELECT * FROM #child
DROP TABLE #child
GO

The # tells SQL Server to create a temp-table.

As a matter of fact, I'd eliminate the cursor too -- insert the first
record into the temp table manually, then use this loop to insert all
the child organizations:

WHILE EXISTS (select * from organization
  where Parent_organization_id in (select id from #child)
  and organization_id not in (select id from #child))
BEGIN
  INSERT INTO #child (id, name, parent)
  SELECT organization_id, organization_nm, Parent_organization_id
  FROM organization where Parent_organization_id in (select id from
#child)
  and organization_id not in (select id from #child)
END


(really #child is a bad name for a table but then, so are "id" and
"name" bad names for columns).

hth

s. isaac dealey   954.522.6080
new epoch : isn't it time for a change?

add features without fixtures with
the onTap open source framework

http://macromedia.breezecentral.com/p49777853/
http://www.sys-con.com/author/?id=4806
http://www.fusiontap.com



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Logware (www.logware.us): a new and convenient web-based time tracking 
application. Start tracking and documenting hours spent on a project or with a 
client with Logware today. Try it for free with a 15 day trial account.
http://www.houseoffusion.com/banners/view.cfm?bannerid=67

Message: http://www.houseoffusion.com/lists.cfm/link=i:4:201391
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54

Reply via email to