Inner cursor performance issues Before I tried to avoid using cursor because of performance issues. But now I have to do some calculations to reach some goals. I have a stored procedure that has a code like this:

DECLARE  Outer_Cursor CURSOR FOR...

   OPEN Outer_Cursor
   FETCH NEXT FROM Outer_Cursor INTO ...

   WHILE @@FETCH_STATUS = 0
   BEGIN

      DECLARE  Inner_Cursor CURSOR  FOR...

      OPEN Inner_Cursor
      FETCH NEXT FROM Inner_Cursor INTO ...

      WHILE @@FETCH_STATUS = 0
      BEGIN

        ...

        FETCH NEXT FROM Inner_Cursor INTO ...
      END

      CLOSE Inner_Cursor
      DEALLOCATE Inner_Cursor    

      FETCH NEXT FROM Outer_Cursor INTO ...

  END

  CLOSE Outer_Cursor
  DEALLOCATE Outer_Cursor


Is it possible to avoid (minimize) so expensive cursor recreation for inner cursor. How can I reuse inner cursor declaration, if it is possible?
Best practice is to do away with cursors. Both of them. There is always a better-performing solution.

If possible, reduce the problem to one that can be solved with sets instead of iteration.

But, if this nested iteration cannot be avoided, you will still get far better performance using table variables and WHILE loops instead of a cursor. For example:

DECLARE @outerData TABLE (outerID INT, outerColumn VARCHAR(50), PRIMARY KEY (outerID))
DECLARE @innerData TABLE (innerID INT, innerColumn VARCHAR(50), PRIMARY KEY (innerID))
DECLARE @currOuter INT
DECLARE @currInner INT

INSERT INTO @outerData
SELECT  EmployeeID, EmployeeName
FROM dbo.Employee

WHILE EXISTS (SELECT outerID FROM @outerData)
BEGIN
    SELECT @currOuter = TOP outerID FROM @outerData

    INSERT INTO @innerData
    SELECT SaleID, SaleDesc
    FROM EmployeeSales 
    WHERE EmployeeID = @outerID

    WHILE EXISTS (SELECT innerID FROM @innerData)
    BEGIN
        SELECT @currInner = TOP innerID FROM @innerData

        --Do work with inner data

        ...blah-de-blah...

        --Delete after work is finished
        DELETE FROM @innerData   WHERE innerID = @currInner
    END

    --Delete current outerID and empty the innerdata table 
    --to prepare to move to the next record

    DELETE FROM @outerData WHERE outerID = @currOuter
    DELETE FROM @innerData
END