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. Delete Query

Delete Query

Scheduled Pinned Locked Moved Database
questiondatabase
4 Posts 4 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.
  • S Offline
    S Offline
    sjs4u
    wrote on last edited by
    #1

    Hi, I am having 2 table. I want to delete records having same ID within 1 query. e.g. ID name table1 1 a table2 1 b How can I do? Thanks

    S D T 3 Replies Last reply
    0
    • S sjs4u

      Hi, I am having 2 table. I want to delete records having same ID within 1 query. e.g. ID name table1 1 a table2 1 b How can I do? Thanks

      S Offline
      S Offline
      subai
      wrote on last edited by
      #2

      if you want to remove only from one table you can use this

      DELETE FROM TABLE_1 WHERE ID IN (SELECT ID FROM TABLE_2)

      or

      DELETE FROM Table_1
      FROM Table_2 AS t2 INNER JOIN
      Table_1 as t1 ON t1.ID = t2.ID

      but i don't know if it's possible to remove records from 2 different tables by one single query

      I Wish the Life Had CTRL-Z Wizard's First Rule : People are fool,they believe what they want to believe or what they afraid to believe www.subaitech.blogspot.com

      1 Reply Last reply
      0
      • S sjs4u

        Hi, I am having 2 table. I want to delete records having same ID within 1 query. e.g. ID name table1 1 a table2 1 b How can I do? Thanks

        D Offline
        D Offline
        Declan Bright
        wrote on last edited by
        #3

        It you have a relationship setup between the 2 tables on the id column with the cascading delete property set, then deleting the records for one table will delete the corresponding records from the other table

        Declan Bright www.declanbright.com

        1 Reply Last reply
        0
        • S sjs4u

          Hi, I am having 2 table. I want to delete records having same ID within 1 query. e.g. ID name table1 1 a table2 1 b How can I do? Thanks

          T Offline
          T Offline
          TheFM234
          wrote on last edited by
          #4

          Write a stored procedure:

          Create Proc usp_DeleteFromTables
          @Id int
          As

          Delete From Table1
          Where Id = @Id

          Delete From Table2
          Where Id = @Id

          Then execute it. For example, if you wanted to delete id # 2:

          Exec usp_DeleteFromTables 2

          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