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
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Deleting record from a parent table

Deleting record from a parent table

Scheduled Pinned Locked Moved Database
5 Posts 3 Posters 0 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
    anujose
    wrote on last edited by
    #1

    Hi, i have a parent and child tables. when i am deleting a row from a parent table i want to check wheter that value in Primarykey column exits in childtables or not(it is a forien key in child tables) without mentioning the child table names.ie; i should be generic..i am using sqlserver 2005. Anu

    C P 2 Replies Last reply
    0
    • A anujose

      Hi, i have a parent and child tables. when i am deleting a row from a parent table i want to check wheter that value in Primarykey column exits in childtables or not(it is a forien key in child tables) without mentioning the child table names.ie; i should be generic..i am using sqlserver 2005. Anu

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Then you will have to write some SQL that examines the dependencies of a table, find the relationships then traverses it and examine the child table. You might be able to wrap this up into a UDF (User Defined Function) but I can't imagine that it would be very quick.


      Upcoming FREE developer events: * Glasgow: db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website

      1 Reply Last reply
      0
      • A anujose

        Hi, i have a parent and child tables. when i am deleting a row from a parent table i want to check wheter that value in Primarykey column exits in childtables or not(it is a forien key in child tables) without mentioning the child table names.ie; i should be generic..i am using sqlserver 2005. Anu

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        If you want it to prevent deletes if there are child rows, why not just make sure that cascading deletes is switched off on your tables?

        Deja View - the feeling that you've seen this post before.

        A 1 Reply Last reply
        0
        • P Pete OHanlon

          If you want it to prevent deletes if there are child rows, why not just make sure that cascading deletes is switched off on your tables?

          Deja View - the feeling that you've seen this post before.

          A Offline
          A Offline
          anujose
          wrote on last edited by
          #4

          How can i switched of cascading deletes. actually i am not deleting the full record from parent table.just i want to check checking whether it contains child rows or not Anu

          P 1 Reply Last reply
          0
          • A anujose

            How can i switched of cascading deletes. actually i am not deleting the full record from parent table.just i want to check checking whether it contains child rows or not Anu

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            OK - the cascading ability is maintained per FK relationship. As you aren't actually performing a delete - you are only doing an update, you are going to have to do this the way that Colin suggested.

            Deja View - the feeling that you've seen this post before.

            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