943,569 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 3197
  • MS SQL RSS
Feb 11th, 2008
0

deleteting multiple rows and related table entries

Expand Post »
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...


MS SQL Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jimshef is offline Offline
2 posts
since Feb 2008
Mar 7th, 2008
0

Re: deleteting multiple rows and related table entries

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


MS SQL Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
space1000 is offline Offline
9 posts
since Mar 2008
Mar 8th, 2008
0

Re: deleteting multiple rows and related table entries

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:

MS SQL Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 5
Light Poster
AaronASterling is offline Offline
31 posts
since Dec 2007
Mar 8th, 2008
0

Re: deleteting multiple rows and related table entries

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.
MS SQL Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jimshef is offline Offline
2 posts
since Feb 2008
Mar 9th, 2008
0

Re: deleteting multiple rows and related table entries

Please post the results. I am curious too see which is fastest. thanks
Reputation Points: 10
Solved Threads: 5
Light Poster
AaronASterling is offline Offline
31 posts
since Dec 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: How do I Backup the MS SQL server 2000 database file (mdf) to my local PC ?
Next Thread in MS SQL Forum Timeline: (Sum(Sum(MarkU)))/5





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC