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