deleteting multiple rows and related table entries

Please support our MS SQL advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 2
Reputation: jimshef is an unknown quantity at this point 
Solved Threads: 0
jimshef jimshef is offline Offline
Newbie Poster

deleteting multiple rows and related table entries

 
0
  #1
Feb 11th, 2008
Hi there.

i would like this procedure to delete all rows in 'sale' where date_modified < (getdate() +0.02) and all the related rows from 'sale_detail' and 'sale_address' i have written the code below, but ti think this might only delete one row from sale and its related rows in the other tables. Is this correct or will it detele all entries from sale etc. If not any suggestions?

Thanks...


  1. CREATE PROCEDURE Sale_DeleteSaleTimedOut
  2.  
  3. AS
  4.  
  5. SET nocount ON
  6.  
  7. DECLARE
  8. @sale_id int
  9.  
  10.  
  11. SELECT @sale_id = sale_id FROM sale WHERE date_modified < (getdate() +0.02)
  12. IF (@sale_id IS NOT NULL) AND (@sale_id <> '')
  13. BEGIN
  14. DELETE sale_detail WHERE sale_id = @sale_id
  15. DELETE sale_address WHERE sale_id = @sale_id
  16. DELETE sale WHERE sale_id = @sale_id
  17. END
  18.  
  19.  
  20. SET nocount off
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 9
Reputation: space1000 is an unknown quantity at this point 
Solved Threads: 1
space1000 space1000 is offline Offline
Newbie Poster

Re: deleteting multiple rows and related table entries

 
0
  #2
Mar 7th, 2008
This will only work if it finds on result in the query "select @sale_id = sale_id from sale where date_modified < (getdate() +0.02)
"

It's better to do it with a cursor


  1. CREATE PROCEDURE Sale_DeleteSaleTimedOut
  2.  
  3. AS
  4.  
  5. SET nocount ON
  6.  
  7. DECLARE
  8. @sale_id int
  9.  
  10. CREATE del_cursor cursor
  11. FOR
  12. SELECT sale_id FROM sale WHERE date_modified < (getdate() +0.02) AND sale_id <> ''
  13.  
  14. open del_cursor
  15.  
  16. fetch next FROM del_cursor INTO @sale_id
  17.  
  18. while @@fetch_status <> 0
  19. BEGIN
  20. DELETE sale_detail WHERE sale_id = @sale_id
  21. DELETE sale_address WHERE sale_id = @sale_id
  22. DELETE sale WHERE sale_id = @sale_id
  23. fetch next FROM del_cursor INTO @sale_id
  24.  
  25. END
  26.  
  27. close del_cursor
  28. deallocate del_cursor
  29.  
  30. SET nocount off
Last edited by peter_budo; Mar 10th, 2008 at 9:26 pm. Reason: Keep It Organized - please use [code] tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 31
Reputation: AaronASterling is an unknown quantity at this point 
Solved Threads: 5
AaronASterling's Avatar
AaronASterling AaronASterling is offline Offline
Light Poster

Re: deleteting multiple rows and related table entries

 
0
  #3
Mar 8th, 2008
Cursors are slooowwwwwww. you can add the ON DELETE CASCADE option to the foreign key in the sale_detail and sale_address tables. This makes it so that whenever you delete an entry in the parent table, all dependent records are deleted in the child tables that are created with this option. alternatively:

  1. CREATE PROCEDURE Sale_DeleteSaleTimedOut
  2. AS
  3. BEGIN
  4. BEGIN TRANSACTION DeleteSaleTimedOut
  5.  
  6. DELETE FROM sale_detail
  7. FROM sale_detail AS d
  8. INNER JOIN sale AS s
  9. ON d.sale_id = s.sale_id
  10. WHERE s.date_modified < (getdate() +0.02)
  11.  
  12. DELETE FROM sale_address
  13. FROM sale_address AS a
  14. INNER JOIN sail AS s
  15. ON a.sale_id = s.sale_ID
  16. WHERE s.date_modified < (getdate() +0.02)
  17.  
  18. DELETE FROM sale AS s
  19. WHERE s.date_modified < (getdate() +0.02)
  20.  
  21. COMMIT TRANSACTION DeleteSaleTimedOut
  22. END

almost anything you can do with cursors you can do with joins or subqueries or temporary tables and all of the above will be faster.
Aaron Sterling
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 2
Reputation: jimshef is an unknown quantity at this point 
Solved Threads: 0
jimshef jimshef is offline Offline
Newbie Poster

Re: deleteting multiple rows and related table entries

 
0
  #4
Mar 8th, 2008
Hi there

thanks for the reply's I worked it out for myself befor anyone replied using the code below, i'm not sure which method will be the quickest? I'll run some tests on the database to have a look.
  1. CREATE PROCEDURE Sale_DeleteSaleTimedOut
  2.  
  3. AS
  4.  
  5. SET nocount ON
  6.  
  7. DECLARE
  8. @sale_id int
  9.  
  10. WHILE EXISTS (SELECT sale_id FROM sale WHERE (date_modified+0.02) < getdate() )
  11. BEGIN
  12. SELECT @sale_id = sale_id FROM sale WHERE (date_modified+0.02) < getdate()
  13. IF (@sale_id IS NOT NULL) AND (@sale_id <> '')
  14. BEGIN
  15. DELETE sale_detail WHERE sale_id = @sale_id
  16. DELETE sale_address WHERE sale_id = @sale_id
  17. DELETE sale WHERE sale_id = @sale_id
  18. End
  19. End
  20.  
  21. SET nocount off
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 31
Reputation: AaronASterling is an unknown quantity at this point 
Solved Threads: 5
AaronASterling's Avatar
AaronASterling AaronASterling is offline Offline
Light Poster

Re: deleteting multiple rows and related table entries

 
0
  #5
Mar 9th, 2008
Please post the results. I am curious too see which is fastest. thanks
Aaron Sterling
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for MS SQL
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC