Overblog
Follow this blog Administration + Create my blog

Posts with #sql storeprocedure category

Jun 16 2012 Jun 16 2012
Post type Text

SQL Hierarchical Data (Simplified)

Posted Jun 16 2012 by Joe Garrett in SQL StoreProcedure

Create The SQL Table {tblTree}:

  • ID {Unique ID Identity Seed}

  • ParentID {bigint}

  • Name{varchar(50)}

Create Stored Procedure {spGenerateTree}

CREATE PROCEDURE spGenerateTree

@lookupid bigint

AS

BEGIN

WITH CTE(name, id, parentid) AS

(

SELECT name, childid, parentid from tblTree where id=@lookupid

UNION ALL

SELECT tree.name, tree.id, tree.parentid

from tblTree tree

INNER JOIN CTE c ON tree.ID = c.parentid

)

SELECT * FROM CTE

END

Populate tblTree with your Tree Data and voila at execution of the PROC you will have a nested iHierarchal Dataset directly from SQL that you can use in your C# ASP.NET Page.

Next page