1

I trying to figure out how I can delete from multiple tables in SQL Server.

I have one table containing only one primary key and three foreign keys for the three table I want to delete from. The other three table does not contain any foreign keys.

The stored procedure have one parameter, a specific primary key from one of the tables. I want to delete from the other tables WHERE tableID = @tableID. The constraints between the tables are set to cascade.

Is it possible with only that parameter to delete from all four tables?

I've tried with inner join, outer join, temptable..

Table      Table        Table      Table 
Pk         Pk(Fk1)      Pk(Fk2)    Pk(Fk3)
Fk1        Column       Column     Column
Fk2        Column       Column     Column
Fk3

I have the table 2 Pk as parameter.

1
  • 1
    Thanks, did not know it existed, will do! Commented Apr 1, 2012 at 19:43

1 Answer 1

3

if your foreign keys were created with DELETE CASCADE, once you delete from the main table, all the related rows on the foreign tables will be delete too.

It seems like this is how it is configured, isnt it working?

Sign up to request clarification or add additional context in comments.

4 Comments

That was what I was thinking, but it's not working. It only deletes from the table containing the primary key that is the parameter.
can you post your table and keys, so we can take a look?
sorry but I did not understand. It would be easier if you just posted the DDL of the tables involved. With the keys.
Solved it, I realized that I made it more complicated than it was.. And I realized that the databas is not well done. Thanks and sorry for taking up your time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.