Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. get all parent tables name with child from sql 2005

get all parent tables name with child from sql 2005

Scheduled Pinned Locked Moved Database
databasequestionsql-serversysadmin
5 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Abdul Rahman Hamidy
    wrote on last edited by
    #1

    Dear All, I am trying to list parent and child tables from sql 2005, but i could not find the solution. well, How can i list all parent tables first then child tables from sql server 2005 in a single statement?

    Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan

    M 1 Reply Last reply
    0
    • A Abdul Rahman Hamidy

      Dear All, I am trying to list parent and child tables from sql 2005, but i could not find the solution. well, How can i list all parent tables first then child tables from sql server 2005 in a single statement?

      Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      You may want to look into the sys objects. This view seems to have some interesting info for you sys.foreign_key_columns it is in 2008 but I think it may be there in 2005 as well.

      A 1 Reply Last reply
      0
      • M Mycroft Holmes

        You may want to look into the sys objects. This view seems to have some interesting info for you sys.foreign_key_columns it is in 2008 but I think it may be there in 2005 as well.

        A Offline
        A Offline
        Abdul Rahman Hamidy
        wrote on last edited by
        #3

        I tried with this query but didnt help

        select distinct(tb.[name]) as TableName,tb.[Object_Id] as ObjectId,fk.type_desc,fk.[type] as [type] from sys.tables tb left join sys.foreign_keys fk
        on tb.[object_Id] = fk.parent_object_Id
        where tb.[type]='U' and tb.is_ms_shipped=0 and tb.[name] not like 'asp%' and tb.[name] not like 'sys%'
        order by fk.[type] asc

        Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan

        M 1 Reply Last reply
        0
        • A Abdul Rahman Hamidy

          I tried with this query but didnt help

          select distinct(tb.[name]) as TableName,tb.[Object_Id] as ObjectId,fk.type_desc,fk.[type] as [type] from sys.tables tb left join sys.foreign_keys fk
          on tb.[object_Id] = fk.parent_object_Id
          where tb.[type]='U' and tb.is_ms_shipped=0 and tb.[name] not like 'asp%' and tb.[name] not like 'sys%'
          order by fk.[type] asc

          Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          This took me 10 minutes of faffing about with the system tables. Take a look at the last 2 columns

          SELECT K.*,'|',O.NAME,p.name
          FROM sys.foreign_keys K
          INNER JOIN sysobjects O ON O.id = K.referenced_object_id
          INNER JOIN sysobjects P ON P.id = K.parent_OBJECT_ID

          A 1 Reply Last reply
          0
          • M Mycroft Holmes

            This took me 10 minutes of faffing about with the system tables. Take a look at the last 2 columns

            SELECT K.*,'|',O.NAME,p.name
            FROM sys.foreign_keys K
            INNER JOIN sysobjects O ON O.id = K.referenced_object_id
            INNER JOIN sysobjects P ON P.id = K.parent_OBJECT_ID

            A Offline
            A Offline
            Abdul Rahman Hamidy
            wrote on last edited by
            #5

            well, i have done this which returns the requirement, but only it does not returns parent tables first then child tables. would u help me to return parent tables first than child tables.

            select distinct(t.name),t.[object_id] from sys.tables t left join sys.foreign_keys k
            on t.object_id=k.parent_object_id
            where t.[type]='U' and t.is_ms_shipped=0 and t.[name] not like 'asp%' and t.[name] not like 'sys%'
            order by t.[name] desc

            Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups