O yes, you are right (as always :o)) there must be a lot of improvements here. To check if the table is opened already, exact matches of the fields, is the TAG existed etc. But this is only an example, I am a little tired (23:00) now and I skip some obvious things :o) to make it easy to myself.
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.