---Select max(cast(dbo.GetFormdataItem(FormData, 11) as int)) from AccDocItem with(nolock) where Left(FormData,5) = 'Cash2'
--select max(dbo.GetFormdataItem(FormData,11)) from AccDocItem  where Left(FormData,5) = 'Cash1' order by id
--select * from AccDocItem  where Left(FormData,5) = 'Cash2'

DECLARE @FormData as nvarchar(1000)
DECLARE @ID as bigint
DECLARE @Num as int
DECLARE @RowNum as int
SET @NUM=1

DECLARE MyCursor CURSOR LOCAL FAST_FORWARD READ_ONLY FOR 
	select ID, FormData from AccDocItem  where Left(FormData,5) = 'Cash2' Order by ID 

OPEN MyCursor
--Cash2;3,42,250,0;3,42,267,0;12/10/2013;100000;همینه دیگه;1;2;3;2;2

FETCH NEXT FROM MyCursor INTO @ID, @FormData
WHILE @@FETCH_STATUS = 0 
BEGIN

	if dbo.GetFormdataItem(@FormData,6) is null 
		set @FormData=@FormData+';' 
	if dbo.GetFormdataItem(@FormData,7) is null 
		set @FormData=@FormData+';0' 
	if dbo.GetFormdataItem(@FormData,8) is null 
		set @FormData=@FormData+';0' 
	if dbo.GetFormdataItem(@FormData,9) is null 
		set @FormData=@FormData+';0' 
	if dbo.GetFormdataItem(@FormData,10) is null 
		set @FormData=@FormData+';0' 
print @FormData
	if dbo.GetFormdataItem(@FormData,11) is null 
	begin
		set @FormData=@FormData+';' +LTRIM(Str(@Num))
		update AccDocItem set FormData=@FormData where id=@ID 
		print @FormData
	end
	else
	begin
		print '************'
		print dbo.GetFormdataItem(@FormData,11)
		if @Num < dbo.GetFormdataItem(@FormData,11)
			set @Num = dbo.GetFormdataItem(@FormData,11)
	end

	set @Num = @Num + 1

	FETCH NEXT FROM MyCursor INTO @ID, @FormData
END

CLOSE MyCursor
DEALLOCATE MyCursor


--Cash1;4,26,4,0;2,22,57,0;02/07/2015;23000000;