Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Detecting field presence in dbf
Message
From
29/07/2007 13:07:15
Mike Yearwood
Toronto, Ontario, Canada
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 9 SP1
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01244177
Message ID:
01244438
Views:
15
Hi Russell

Thanks for that.

I'm not a big fan of double duty things. I'd make two routines, 1) IsFieldInTable and 2) GetFieldPosition.

As you note, because it does double duty, you're unable to merely interrogate if the field is there, causing it to be slow.



>
>*------------------------------------------------------------------------------
>* Procedure :  IsFieldInTable
>* Purpose   :  This procedure will return the position of a field in a table
>*              if the field exists in the table, otherwise it will return a zero.
>*
>* NOTE      :  This function could be made a little more efficient via the
>*              use of the FIELD function, however this function does double
>*              duty by indicating if the field exists (nonzero return value) and
>*              the position of the field in the table, which some of the calling
>*              routines make use of.
>*
>* Programmer:  Russell Campbell
>* Copyright :  The Interthink Consulting Group, Inc. - All Rights Reserved
>* Date      :  10/29/2001
>* Revisions :
>*------------------------------------------------------------------------------
>procedure IsFieldInTable
>lparameters tcFieldName, tcTableName
>	
>	local RtnVal
>	local array MyFieldsArray(1)
>	
>	if empty(tcTableName)
>		tcTableName = alias()
>	endif
>	
>	RtnVal = -1
>	
>	if afields(MyFieldsArray, tcTableName) > 0
>		RtnVal = ascan(MyFieldsArray, alltrim(tcFieldName), -1, -1, 1, 7)
>		if RtnVal > 0
>			RtnVal = asubscript(MyFieldsArray, RtnVal, 1)
>		endif
>	endif
>	
>return RtnVal
>
>
>>>>Hey all
>>>>
>>>>Some time ago I think Fabio Lunardon demonstrated using fsize to check for the presence of a field. What thread was that? I can't find it though I looked.
>>>>
>>>>Considering the need to manage SET COMPATIBLE for a UDF to use FSIZE I can't see it being the fastest way.
>>>>
>>>>I have used a UDF based on TYPE() and it is faster than a UDF using FSIZE that also handles setting and resetting SET COMPATIBLE. I think I found FIELD() is faster.
>>>>
>>>>There is another consideration. Using a UDF can be slow, so using an idea that is in my FoxPro Advisor article: http://advisor.com/doc/17440, the command that can detect a field with the fewest external dependencies could be directly injected into code. It seems the easiest and fastest is to use FIELD().
>>>>
>>>>Did FIELD() always accept the fieldname and alias?
>>>
>>>Probably best one is:
>>>field(upper(m.Field)) == upper(m.Field)
>>>
>>>fsize() is better than Type() thinking reliability. Afields() + ascan() is yet one of the best IMHO.
>>
>>aFields + ascan is too much work. It cannot be executed as a single expression and must be a UDF or a few lines of code.
Previous
Reply
Map
View

Click here to load this message in the networking platform