Can a DELETE statement reference another database

  • I want to delete data in a table in database 1, but reference a table in database 2

    i.e. DELETE TABLEX INNER JOIN DATABASEZ.DBO.TABLE1 ON TBXRefNo = DBZRefNo

    WHERE (DBZRefName = 'XYZ')

    Is this possible? Will the above code work? So far it hasn't

  • That would work once the columns' name are corrected.

    DELETE X FROM TABLEX X INNER JOIN DATABASEZ.DBO.TABLE1 T2 ON X.TBXRefNo = T2.DBZRefNo

    WHERE (T2.DBZRefName = 'XYZ')

  • yup.

    or

    DELETE TABLEX

    WHERE TBXRefNo IN (SELECT DBZRefNo

    FROM DATABASEZ.DBO.TABLE1

    WHERE DBZRefName = 'XYZ')

  • Actually I got it to work as follows:

    DELETE FROM TABLEX

    FROM TABLEX INNER JOIN DATABASEZ.DBO.TABLE1 ON TBXRefNo = DBZRefNo

    WHERE (DBZRefName = 'XYZ')

    Once I added the FROM TABLEX it worked beautifully.

    Thanks for your help

  • Hi,

    You need not use FROM TABLEX twice which is redundant and also makes the query unclear.

    Whatever Ninja suggested works well.

    Regards,

    Durga

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply