Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Hi,
Can anyone help me with result from FieldName() function that is sometimes returning a blank instead of a fieldname?
I have a function that goes through list of columns in a table to remove temporary columns. In my case the temporary columns all have fomat <name>.tmp. Script is below.
The problem that I'm having is two of the columns are getting missed, and when i analyse why, I'm finding that FieldName() function is returning a blank result for them.
Here is the script:
for i=1 to NoOfFields('$(tableName)')
vFieldName=FieldName($(i),'$(tableName)');
trace fieldNumber=$(i),tableName=$(tableName),vFieldName=$(vFieldName);
if wildmatch('$(vFieldName)','*.tmp') then
drop field $(vFieldName) from $(tableName);
end if;
next;
Comment the if statement and you will see the difference:
// if wildmatch('$(vFieldName)','*.tmp') then
// drop field $(vFieldName) from $(tableName);
//end if
This is because you cannot drop the columns in the table using for loop.
Once you dropped the columns, the fieldname() function will differs as in original.
Eg:
In the ori table, it has 3 columns [a,b,c].
When the if statement is true, the number of columns -1 due to drop statement. [b,c]
In the for loop, number of columns still remains as 3, which is incorrect as the number of columns now reduced to 2.
What is the column name that is missing?
Comment the if statement and you will see the difference:
// if wildmatch('$(vFieldName)','*.tmp') then
// drop field $(vFieldName) from $(tableName);
//end if
This is because you cannot drop the columns in the table using for loop.
Once you dropped the columns, the fieldname() function will differs as in original.
Eg:
In the ori table, it has 3 columns [a,b,c].
When the if statement is true, the number of columns -1 due to drop statement. [b,c]
In the for loop, number of columns still remains as 3, which is incorrect as the number of columns now reduced to 2.
Thanks Arther,
So I need to do the forloop to get the list of fields that I want to drop, and then drop them as a second step once I'm outside the for loop.
Or perhaps, I could drop the the field inside the for loop, and then decrement the value of $(i) for each field dropped... (I'm guessing it'd probably work based on the behaviour I've seen, but I'm not sure I'd consider it good coding practice).