ALTER PROCEDURE _Cursor
AS
BEGIN TRAN
DECLARE @IdObj int
DECLARE @IdTblDolsh int
DECLARE @IdIstInv int
DECLARE @IdDolshPlanFin int
DECLARE CurDolPlanFin CURSOR FOR
SELECT IdTblDolsh FROM TblPlanPokazateli
OPEN CurDolPlanFin
FETCH NEXT FROM CurDolPlanFin INTO @IdDolshPlanFin
WHILE @@FETCH_STATUS =0
BEGIN
IF @IdDolshPlanFin NOT IN (SELECT IdTblDolsh FROM TblDolsh)
BEGIN
DELETE FROM TblPlanPokazateli WHERE IdTblDolsh=@IdDolshPlanFin
END
FETCH NEXT FROM CurDolPlanFin INTO @IdDolshPlanFin
END
CLOSE CurDolPlanFin
DEALLOCATE CurDolPlanFin
DECLARE CurDol CURSOR FOR
SELECT IdTblDolsh,IdObj FROM TblDolsh
OPEN CurDol
FETCH NEXT FROM CurDol INTO @IdTblDolsh, @IdObj
WHILE @@FETCH_STATUS = 0
BEGIN
IF @IdTblDolsh NOT IN (SELECT IdTblDolsh FROM TblPlanPokazateli)
BEGIN
DECLARE CurIstInv CURSOR FOR
SELECT IdIstInv FROM TblKlassIstInv
OPEN CurIstInv
FETCH NEXT FROM CurIstInv INTO @IdIstInv
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO TblPlanPokazateli (IdObj,IdTblDolsh,IdIstFin,Smet,Dom,Kv,Plosh,OstSmet) VALUES (@IdObj,@IdTblDolsh,@IdIstInv,0,0,0,0,0)
FETCH NEXT FROM CurIstInv INTO @IdIstInv
END
CLOSE CurIstInv
DEALLOCATE CurIstInv
END
FETCH NEXT FROM CurDol INTO @IdTblDolsh, @IdObj
END
CLOSE CurDol
DEALLOCATE CurDol
COMMIT
RETURN
|