But here I need function which will return a random value of 7 digits which will be check in the table for a particular column value. If that number is not present it will return the number generated else it will generate a new number.Then the initialization code simply changes:
SQL> insert into t
2 select rownum, tag from t1;
to
SQL> insert into t
2 select rownum, tag from t1
3 where tag not in ( select ... from existing_table )
Now you have a list of the all of the unused tags. From that point on, you follow the approach I listed and you never need to "check" again