ALTER Procedure [dbo].[P_CopyUebungsbaum] (@FromID int,@ToID int) as
/*
20080227 by Thomas Wassermann
Problem Cursorname bei Recursionen daher EXEC(@
SQL)
*/
Declare @
SQL Varchar(8000)
Select @
SQL=
'
Declare @NewID int
Declare @x int
Insert Into Uebungsbaum (Beschreibung,ParentID,Sort) Select Beschreibung,@ToID,Sort from Uebungsbaum where ID=@FromID
Select @NewID=Ident_Current(''Uebungsbaum'')
INsert into Uebungsbaum_Uebungen (Uebungsbaum_id,Uebungen_IDX) select @NewID,Uebungen_IDX from Uebungsbaum_Uebungen where Uebungsbaum_id=@FromID
DECLARE My_Cursor CURSOR FOR
SELECT ID FROM Uebungsbaum WHERE ParentID=@FromID
OPEN My_Cursor
FETCH NEXT FROM My_Cursor
INTO @x
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC P_CopyUebungsbaum @x,@NewID
FETCH NEXT FROM My_Cursor
INTO @x
END
CLOSE My_Cursor
DEALLOCATE My_Cursor
'
Select @
SQL=Replace(@
SQL,'My_Cursor','My_Cursor'+Cast(@FromID as Varchar(10)))
Select @
SQL=Replace(@
SQL,'@FromID',Cast(@FromID as Varchar(10)))
Select @
SQL=Replace(@
SQL,'@ToID',Cast(@ToID as Varchar(10)))
EXEC(@
SQL)