Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 1st, 2004, 05:46 PM
Authorized User
 
Join Date: Sep 2003
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
Default Stored procedure timing out

Hi,

I have a stored procedure problem. I have a C++ application that calls a stored procedure. However, many times the application just terminates as the stored procedure times out. It does not happen all the times, but quite frequently (~30%). It never happens when I run the stored procedure from the SQL server osql console app. I do not understand why it should time out. Any suggestions on this would be highly appreciated.

Here is the stored procedure:

--------------------------------------------------------------------
CREATE PROCEDURE DeleteTicketData
@ticketNum int
AS
declare @testVorgangID int
-- delete errors
DELETE FROM TestVorgangEinleseFehler_T WHERE TicketNumber = @ticketNum
Select @testVorgangID = TestVorgangID from TestTickets_T WHERE TestTicketID = @ticketNum
IF (@testVorgangID IS NOT NULL)
BEGIN
  declare @testID int
  DELETE From TestVorgaenge_T where TestVorgangID = @testVorgangID
  DECLARE tests_cursor CURSOR FOR
  SELECT TestID FROM Tests_T WHERE TestVorgangID = @testVorgangID
  Open tests_cursor
  -- Perform the first fetch
  FETCH NEXT FROM tests_cursor INTO @testID
  -- as long as there are more rows to fetch.
  WHILE @@FETCH_STATUS = 0
  BEGIN
    DELETE FROM TestAktionen_T Where TestID = @testID
    DELETE FROM TestDSPACETable_T Where TestID = @testID
    DELETE FROM TestEreignisse_T Where TestID = @testID
    DELETE FROM TestZustaende_T Where TestID = @testID DELETE Tests_T WHERE CURRENT OF tests_cursor
    FETCH NEXT FROM tests_cursor INTO @testID
  END

  CLOSE tests_cursor
  DEALLOCATE tests_cursor
  END
GO
--------------------------------------------------------------------

I am totally puzzled as to why it should time out some of the times. Any suggestions would be really appreciated.

Thanks and have a great new year everyone :)

Pankaj

 
Old January 2nd, 2004, 10:39 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Why are you using a cursor for these delete queries? Some simple joins and/or IN selects should do the trick as well.

Cursors are usually pretty slow, so maybe that's what's causing the time-out.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old January 2nd, 2004, 11:44 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Another alternative is you could store the ID's that you want to delete into a table variable or temp table, and then do a:

DELETE FROM TestAktionen_T Where TestID in (select ID from @tablevariable)

Not sure if this is what you are looking for.....

Brian
 
Old January 2nd, 2004, 12:55 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

DELETE operations in SQL Server can be inherently slow; you are not helping things by using a cursor as well. As Imar suggested, you don't need a cursor - simply JOIN each of your tables to the 'source' table. The results of the JOIN then specify the rows to be deleted. For example:
Code:
DELETE FROM TestAktionen_T
    INNER JOIN Tests_T ON Tests_T.TestID=TestAktionen_T.TestID
    INNER JOIN TestTickets_T ON Tests_T.TestVorgangID=TestTickets_T.TestVorgangID
WHERE TestTickets_T.TestTicketID=@ticketNum

etc.
The DELETE operation must maintain all the index entries on all the rows being deleted, if you have a lot of indexes, this will take some time. If there is other user activity, the DELETE may be blocking until it is able to obtain exclusive locks and this will take time as well.

You might try extending the query timeout on the command object.

Removing the cursor should speed things up considerably, though.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old January 2nd, 2004, 03:53 PM
Authorized User
 
Join Date: Sep 2003
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Cool.

I will first try removing the cursor object. Maybe that is what is making things behave strange.

Thanks everyone for the replies. I really appreciate it. You guys are the best :-)

Pankaj






Similar Threads
Thread Thread Starter Forum Replies Last Post
stored procedure prashant_telkar SQL Server 2000 1 July 9th, 2007 07:57 AM
Help On Stored Procedure desireemm SQL Language 2 October 31st, 2005 07:11 PM
please help me with this stored procedure bebeko SQL Server 2000 1 June 10th, 2005 12:09 PM
stored procedure kvanchi ADO.NET 1 December 9th, 2004 07:27 AM
Stored Procedure... babloo81 SQL Server 2000 2 May 1st, 2004 11:25 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.