SQL Server delete statement

Posted on 2007-07-28
Last Modified: 2010-03-19
I have a database I inhetited

There is a UID in a table that gets copied into other tables on certain transactions.

I would like to know how I can delete from multiple tables where pmrID = # with one SQL statement
Question by:lrbrister
    LVL 33

    Expert Comment

    try this

    2 tables
    delete table1, table2 from table1, table2 where table1.pmrID = 123 and (table1.pmrID = table2.pmrID)

    3 tables
    delete table1, table2, table3 from table1, table2, table3 where table1.pmrID = 123 and (table1.pmrID = table2.pmrID or table1.pmrID = table3.pmrID)

    And so on...
    LVL 3

    Accepted Solution

    You can not delete from multiple tables in one sql statement. But you can specify the "on delete cascade" option on foreign keys.

    ALTER TABLE YourTable
    ADD CONSTRAINT fk_YourTable
    FOREIGN KEY (YourField) REFERENCES YourOtherTable(YourField)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
    In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now