I am facing problem in calling cursor into cursor, it's throwing "A cursor with the name 'ParentDetail' already exists."
Here is my SP, Please have a look :
Alter PROCEDURE FetchChild
@ChildID Int
AS
BEGIN
SET NOCOUNT ON;
Create Table #AllChilds
(
ID Int,
Name Varchar(100)
)
Declare @ID Int
Declare @Name Varchar(100)
Declare ParentDetail Cursor For
Select ID,Name From Contacts Where ISNULL(ParentID,0) = @ChildID
Open ParnetDetail
Fetch Next From ParentDetail Into @ID,@Name
While @@FETCH_STATUS = 0
Begin
Insert Into #AllChilds
Values(@ID,@Name)
Insert Into #AllChilds
EXEC FetchChild
@ChildID = @ID
Fetch Next From ParentDetail Into @ID,@Name
End
Close ParentDetail
Deallocate ParentDetail
Select * From #AllChilds
END
GO