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 Records using two different Databases

Deleting Records using two different Databases

Scheduled Pinned Locked Moved Database
question
4 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.
  • B Offline
    B Offline
    bwhittington
    wrote on last edited by
    #1

    Is it possible to delete a set of records by using WHERE clauses that access tables in two separate databases? I've looked all over the net and I cannot find a straight answer. Thanks in advance

    Brett A. Whittington Application Developer

    P 1 Reply Last reply
    0
    • B bwhittington

      Is it possible to delete a set of records by using WHERE clauses that access tables in two separate databases? I've looked all over the net and I cannot find a straight answer. Thanks in advance

      Brett A. Whittington Application Developer

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

      Here's a quick sample:

      DELETE Item
      FROM table1 WHERE MyValue IN (
      SELECT MyValue2 FROM Database2.dbo.table2 WHERE MyCondition = 'Hello'
      )
      

      The trick is to prepend the name of the database to the schema (the Database2.dbo bit in this sample).

      Please visit http://www.readytogiveup.com/ and do something special today. Deja View - the feeling that you've seen this post before.

      B 1 Reply Last reply
      0
      • P Pete OHanlon

        Here's a quick sample:

        DELETE Item
        FROM table1 WHERE MyValue IN (
        SELECT MyValue2 FROM Database2.dbo.table2 WHERE MyCondition = 'Hello'
        )
        

        The trick is to prepend the name of the database to the schema (the Database2.dbo bit in this sample).

        Please visit http://www.readytogiveup.com/ and do something special today. Deja View - the feeling that you've seen this post before.

        B Offline
        B Offline
        bwhittington
        wrote on last edited by
        #3

        Thanks for the quick response. My problem was how I was trying to use the 2nd database in the where clause. I was not trying it in an IN statement as your example shows. Awesome! :-D

        Brett A. Whittington Application Developer

        C 1 Reply Last reply
        0
        • B bwhittington

          Thanks for the quick response. My problem was how I was trying to use the 2nd database in the where clause. I was not trying it in an IN statement as your example shows. Awesome! :-D

          Brett A. Whittington Application Developer

          C Offline
          C Offline
          Chris Rickard
          wrote on last edited by
          #4

          Same principal applies. Pretend it's exactly like a table in the same database but put a [Database].[Schema] in front of it like "database2.dbo.table"

          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