• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 945
  • Last Modified:

Call Stored Procedure recursively

I am using SQL Server 2000.

I have a Stored Proc that calls itself using a CURSOR. SOmething like:

CREATE Procedure dbo.gui_usp_Delete_ID
      @id int
AS

      --Get a list of non-reference'd child user scenarios
      SELECT
            innerId
      INTO
            #tmpTable
      FROM
            myTable
      WHERE
            id = @id

      --DO what is needed here      
      
      --Recursively delete all the Inner Ids
      DECLARE csrInnerIDs CURSOR
      READ_ONLY
      FOR SELECT innerId FROM #tmpTable

      DECLARE @id int
      OPEN csrInnerIDs

      FETCH NEXT FROM csrInnerIDs INTO @id
      WHILE (@@fetch_status <> -1)
      BEGIN
            IF (@@fetch_status <> -2)
            BEGIN
                  EXEC dbo.gui_usp_Delete_ID @id
            END
            FETCH NEXT FROM csrInnerIDs INTO @id
      END

      CLOSE csrInnerIDs
      DEALLOCATE csrInnerIDs
      
END


And it ofcourse throws an error saying:
A cursor with the name 'csrInnerIDs' already exists.
The cursor is already open.
A cursor with the name 'csrInnerIDs' does not exist.
A cursor with the name 'csrInnerIDs' does not exist.
A cursor with the name 'csrInnerIDs' does not exist.

How do I fix this? Is there a different way to do this recursion?

Thanks
0
srinivas_vemla
Asked:
srinivas_vemla
2 Solutions
 
Ved Prakash AgrawalDatabase Consultant/Performance ArchitectCommented:
hi, can you tell me why you need to execute procedure like this way????
0
 
srinivas_vemlaAuthor Commented:
My table structure has IDs and innerIDs... InnerIDs are nothing but other IDs used by the original ID... and this inner'ness is not limited to 2 levels... i means the innerIDs can again refer to other innerIDs and so on...

So when I want to delete the original ID, i have to recursively delete even the innerIDs associated with them... So i want to call the SP recursively...
0
 
Christopher KileCommented:
Why not define innerID as a foreign key onto Mytable with ON DELETE CASCADE? or are you doing something other than deleting the entire record?

As an alternative, instead of

DECLARE csrInnerIDs CURSOR READ_ONLY FOR SELECT innerId FROM #tmpTable

you might try:

DECLARE @csrInnerIDs CURSOR

SET @csrInnerIDs = CURSOR READ-ONLY FOR SELECT innerId FROM #tmpTable

Each cursor generated should then have a unique instance name.
0
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
Aneesh RetnakaranDatabase AdministratorCommented:
Replace that cursor with some Table variable


CREATE Procedure dbo.gui_usp_Delete_ID
      @id int
AS

      --Get a list of non-reference'd child user scenarios
      SELECT
            innerId
      INTO
            #tmpTable
      FROM
            myTable
      WHERE
            id = @id

      --DO what is needed here      
      DECLARE @tot int
      --Recursively delete all the Inner Ids
      DECLARE @csrInnerIDs TABLE (i INT IDENTITY, innerId INT)
      INSERT INTO @csrInnerIDs       
      SELECT innerId FROM #tmpTable
      SET @tot = @@ROWCOUNT

 
      WHILE (@tot > 0)
      BEGIN
              SELECT @id = innerId FROM #tmpTable WHERE i = @id
                  EXEC dbo.gui_usp_Delete_ID @id
              SET @tot = @tot -1
      END
END
0
 
dqmqCommented:
I think you have more problems with the temp table than with the cursor.  Don't see why you need the temp table, anyway--just select for @ID in the cursor.

But, perhaps a better way is to use add self-referencing foreign key to the table and specify CASCADE DELETE. No programming required!. Put the foreign key on the innerID column referencing the ID column. The idea is that you can then delete the parent and all the children go with it.

Alternatively, you could write a recursive trigger to do the same.
0
 
srinivas_vemlaAuthor Commented:
I am going the CASCADE way... thanks guys
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now