Matsta,
It seems like you want to delete a field (column) from a table's structure. Am I right? If so,there are several ways to do so. And what do you mean by "empty"? All records have NULL value in it? Or just '' for strings, {//} for date, 0 for numerics, etc.? Let's assume for simplicity thatyou mean NULL. Here is some code that first came to mind (not tested, but should work).
SELECT MyTable
COPY STRUCTURE EXTENDED TO MyStru
USE MyStru IN 0
SELECT MyStru
SCAN
SELECT DISTINCT &Field_Name ;
FROM MyTable ;
WHERE !IsNull() ;
INTO ARRAY tmp
IF _TALLY=0
DELETE IN MyStru
ENDIF
ENDSCAN
PACK
CREATE NewTable FROM MyStru
APPEND FROM DBF("MyTable"
This way, you get everything in the new table less the "empty" fields. If you want, you can close and rename them, etc. If you didn't mean NULL, then you will need to check also Field_Type, and compare the values in MyTable to 0, and use Empty() function, where applicable.
You might also want to check alternative solutions, probably using AFields() function and CREATE TABLE FROM ARRAY (SQL command), and, possibly, more. I am not sure
if ALTER TABLE (SQL) can help you delete fields, I don't usually use it.
I got the exact same structure as before when I used above
program. I have a simple table with 5 columns in it and I want to first see if they are completely populated or not..if they are not populated I need to delete them from my table.
.. The columns are numeric.
This is what I was doing before and it worked. ButI was looking for a program that would automatically delete if columns were not populated without browsing each time for a manual look.
select 'myfield' from 'mytable' where 'myfield' > 0
**by browsing this would tell me if my field was populated
**if it was I'd leave it as is
**if it wasn't then I'd....
Matsta,
If you want to delete columns where all the values are zeros, then you need to change just one small thing in my example:
SELECT MyTable
COPY STRUCTURE EXTENDED TO MyStru
USE MyStru IN 0
SELECT MyStru
SCAN
SELECT DISTINCT &Field_Name ;
FROM MyTable ;
WHERE &Field_Name.#0 ;
INTO ARRAY tmp
IF _TALLY=0
DELETE IN MyStru
ENDIF
ENDSCAN
PACK
CREATE NewTable FROM MyStru
APPEND FROM DBF("MyTable"
Or you can use ALTER TABLE, as you did (even shorter code).
I didn't have time to test this, either.
SELECT MyTable
noFields=AFIELDS(theFields)
FOR i=1 TO noFields
theName=theFields(1)
SELECT DISTINCT &theName ;
FROM MyTable ;
WHERE &theName.#0 ;
INTO ARRAY tmp && check, if there are values that <>0
IF _TALLY=0 && nothing found
ALTER TABLE MyTable DROP COLUMN theName
ENDIF
NEXT i
Oh, and I made a mistake in my first example (though irrelevant in your case). It should have been
... WHERE !IsNull(&Field_Name) ...
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.