recursive logic with sql server

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cric098
    New Member
    • Jan 2010
    • 1

    recursive logic with sql server

    I was able to write a recursive logic in c# and because of performance issue I need to write this in Stored Proc or UDF in SQL Server. I have following tables and I would appreciate if any one can suggest me how to do something like this
    Table EQ- ID, Name, SYSID (nullable)
    Table RD - ID, Name
    Table EQ_RD_Xref - EQ_RD_XrefID, EQID, RDID (EQ and RD have many to many relationship)
    Table SMAST - MASTERID, MasterName
    Table EQ_SMAST_XREF - EQ_SMAST_XREF,M ASTERID,EQID
    Table SYS - SYSID, SYSName

    Whenever I want to get the dependencies for EQ BY ID it should give me entire forest of EQ's (tied to EQ_SMAST_XREF and these EQ's may have SYS or other RD's), RD's (these RD's may have some other EQ's) and Sys. The forest should have only the items in the forest and not any other item
    Thanks for any help in advance
  • Delerna
    Recognized Expert Top Contributor
    • Jan 2008
    • 1134

    #2
    Recursive logic is handled in TSQL via the Cursor, so try searching SQL servers help documentation for "Cursor".

    Bear in mind that, for performance reasons, the use of cursors is frowned upon.
    There are situations where a cursor is the only means by which a task can be accomplished but these are rare. It is almost always possible to do something via vastly more efficient, "set" based queries where the recordset is processed as a whole instead of one record at a time.

    Comment

    • nbiswas
      New Member
      • May 2009
      • 149

      #3
      Also you can use Recursive CTE's if you are using Sql Server 2005+

      Here is an example

      SQL SERVER – Simple Example of Recursive CTE

      Comment

      • nbiswas
        New Member
        • May 2009
        • 149

        #4
        You may also find interest in Recursive Stored Procs.

        Using recursion in stored procedures

        Comment

        Working...