Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if FIELD exists on a table? 1

Status
Not open for further replies.

diarmaid

Programmer
Jun 27, 2003
34
IE
How can I check if a particular table has a field of a specific name ? If I find it doesn't exist I need to create it using ALTER TABLE

Many thanks,

Diarmaid
 
Check the AFIELDS() function. This will build an array of your table structure. You can then loop through the array checking for the existence of your field name.

Neil

I like work. It fascinates me. I can sit and look at it for hours...
 

Many thanks, I also found an earlier thread on this question and it was interesting. Also, I have found reference to the functions field() and fcount()

fcount() is the total number of fields in a table, so one can loop through them by number 1 to fcount(), also,

field(i) will return the field name

Diarmaid
 
I wold suggest the following in a function, program or method:
Code:
LPARAMETERS MyField
AFIELDS(TblFields)
RETURN ASCAN(TblFields, MyField)

That's the basics - as it is it would work on whatever table or cursor is open in the current work area.

It could be improved by adding a 2nd parameter for a table-name (with the option to work on the current area if no table-name is received). If you were to do that, remember to add code to change work area before opening the new table and to return to the current work area afterwards.

I had a look at DBGETPROP & CURSORGETPROP but they don't appear to do what you want.

Hope that helps,

Stewart
PS If you want to get the best response to a question, please check out FAQ184-2483 first.
 
You may simply try:

Code:
select 0
use mytable in 0 exclusive
if type("mytable.mynewfield") # cTypeexpected
   alter table mytable add mynewfield ...
endif

Bye, Olaf.
 
Another one suggestion would be to use

FSIZE(fieldname)

If it returns 0, it does not exist.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top