How to Create Recursive Function ?

How to Create Recursive Function ?


In my table, I have an attribute Index. Whenever I am creating new record, I am assigning value to Index as TableLength + 1.

I was getting Index number for every record as 1,2,3,4.....

Problem happening is that if I deleted record no 3, and added new record I am getting Index sequence as 1,2,4,4.....

I want to check whether particular Index is present or not and if that Index is not present, create that Index.

Suppose, I have Index sequence as 1,3,4....
First I will compare 1 is present in table or not. If present then start search for 2.
2 is not present then stop the loop and assign 2 as Index value.

I am not able to start foreach loop from first position once my comparison is finished for Index 1.

Please advice how to iterate through loop unless I get Index which is not matching. How to create Recursive function for this as well.

Thanks and Regards,
Suraj Borade
What if you don't delete the record?
  • Use the auto-increment to get all the ids sequential.
  • Create an isDeleted attribute to mark the ones deleted (instead of really deleting them)
  • Query for the first isDeleted in table and Update record.

The other alternatives I see are: a very complex variable with a list of deleted ids; or to dynamically generate a table will all ids until "table length +1", join, and search for the holes.

You seem to be opting for a sequential read of the entire table, which is pretty bad performance-wise. What if the table contains a few million records, without any gaps? Inserting a record will take a long time.

Also, if you keep Ids sequential, the highest entry will also be the youngest (so sorting by Id gives you the records in chronological order).

So what is your use case? Why have this special Id? Why not just use auto-increment Ids?
Hi Suraj Borade,
             First i will say that its not a best prectice, if you really want then you can achieve it like...
 1:- before inserting get the max index  and add 1 which will be your new index, in this way you will not get the index like 1,2,3,4,5 but it will prevent you to create a duplicate index.

2:-Get the list of index in  asc order, then Isfound =false then treverse the loop like   if(Isfound =false and  lstindex.list.current.tablename.index > lstindex.list[lstindex.list.current.Rownumber-1].tablename.index+1) 

then Isfound var_index=lstindex.list.current.tablename.index +1 and Isfound=true
dont forget to pass the loop if the currentrownumber =0

I hope it will help you.

Hello Kilian Sir,

Actually this logic is implemented by someone else and now it has come to me to fix the bug. This app is already in Production. Previous developer has implemented logic. 

Scenario is as follows:
1. Create New Project Request and save it.
2. Once New Project is created, we are adding info about this project.
3. Here we are implementing one to many relationship. e.g. One Project has multiple Calibrations.
4. Whenever new Calibration is added in Project, Index is also getting created as TableLength+1.

This is working fine. Problem starts when user deletes the Calibration record.

We have implemented soft delete i.e. We have used IsDeleted flag and whenever Caliration record is deleted IsDeleted flag will be set to true.

Next time at the time of adding new record, length of table gets decreased by one since we are displaying records having IsDeleted = false, because of this Index value gets duplicated.

e.g. If there are 4 Calibrations records and user has deleted 3rd, list screen would be like this

Now if I add one New Calibration, list screen will become like this.

This is because when new record is added its Index value will be assigned as TableLength + 1, so I want to avoid this and want to insert Index which is missing in this sequence.

If I start displaying here AutoNumer values, will it be in sequence whenever user deletes any record and adds new record?

I am not hard deleting records, I am just setting IsDeleted flag to true when it gets deleted. I am trying your approach. Thanks ..!

I am trying your approach. Thanks..!
If you do a soft delete, you should not try to reuse Id's, as you will get clashes. What you should do is when storing a new record, query the database to find out the current highest Id (e.g. with a MAX(Id)), and then + 1.

P.S. Hit the other developer over the head that created this mess :).