How to Navigate Up the Tree With MS SQL

"Navigating up or down a tree" is a term used in hierarchical queries. You use the "start with" MS SQL keyword statement to manipulate the way you drill down into the query tables. You start with zero to navigate down the tree. You start with the last numeric value to navigate up the tree, which means you display values in descending order in the hierarchical table tree.

Instructions

    • 1

      Open the SQL Server Management Studio software located in the "SQL Server" program group in the Windows menu. Log in to the database that you want to use to create the query.

    • 2

      Click the database in the left navigation panel to activate it. Click "New Query" to open an editor that points to the database you clicked.

    • 3

      Create the "select" query to determine the records you want to retrieve. For instance, the following query returns a list of employees and their associated organization number:

      SELECT name, organization
      FROM organization o, name emp
      WHERE o.organization_id = emp.org_child_id

    • 4

      Add the "start with" statement to set the navigation in descending order. Type the following code into the editor:

      START
      WITH emp.org_parent_id = 20
      CONNECT

    • 5

      Press the "F5" key to execute the query. The query executes and the results display at the bottom of the window. The navigation works up the tree to the main, top table in the hierarchical data.

Related Searches:

References

Comments

Related Ads

Featured