USE [PH20110829]
GO

/****** Object:  StoredProcedure [dbo].[Feladattakaritas5]    Script Date: 09/09/2011 12:43:10 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[Feladattakaritas5] AS
	-- Add the parameters for the stored procedure here
	declare @RegistryHeader varchar(250), @Count int, @RegistryTask varchar(250), @Edit int
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

   declare cRegistryHeader cursor for select t1.RegistryHeader, Count(RegistryHeader) from RegistryTasks as t1 join
Task as t2 on t1.Oid=t2.Oid where t2.GCRecord is null and t2.Status='0' group by RegistryHeader having COUNT(RegistryHeader) >1
open cRegistryHeader

fetch next from cRegistryHeader
into @RegistryHeader, @Count
begin transaction
while @@FETCH_STATUS = 0
BEGIN
	set @edit = 0
	declare cRegistryTasks cursor for select t1.oid from RegistryTasks as t1 join Task as t2 on t1.oid=t2.oid where t1.registryheader = @registryheader and t2.gcrecord is null and t2.status='0' order by t2.startdate desc 
	open cRegistryTasks
	fetch next from cRegistryTasks into @RegistryTask
		while @@FETCH_STATUS =0
		begin
		
		if @Edit=1 
		update Task set gcrecord='11' where Oid = @RegistryTask 
		
		else
		set @edit = 1
		fetch next from cRegistryTasks into @RegistryTask
		end
		
		close cRegistryTasks
		deallocate cRegistryTasks
		
		fetch next from cRegistryHeader into @RegistryHeader, @Count
END
commit transaction
close cRegistryHeader
deallocate cRegistryHeader

GO

