Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
ALTER TABLE and INSERT INTO not working
Message
From
13/12/2006 20:50:00
Michael Gass
Instructional Software, Inc.
Kirkland, Washington, United States
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
01177095
Message ID:
01177452
Views:
13
Borislav -

That worked beautifully. I would have thought more was wrong. Thank you.

I've include the code below should others be in need of a method to import data from external tables to tables in the current database when fields are exactly the same and when additional update fields have been added to the end of the field list. It includes ZAP to quickly remove data from tables in the current database, so people need to be aware of the dangers involved. In prior code I notify users that all data in current tables will be lost and give them an option to backup data prior to starting the process. Also, it doesn't work for long field names and some of the code is CODEMINE.

Thanks again Borislav!

- Mike
*===============================================================
* Program:         Table Importer
* Purpose:         generic import code
* Author:          Michael Gass
* Email:           support@instructionalsoftware.us
* Copyright:       Offered as public domain on the UT
* Last revision:   12/06/2006
* Purpose
*        The number and names of import and current tables fields must match
*        Determine which fields need to be added to the imported tables
*        Add fields to import tables so number, name, type, and size match
*        fields already in the DBF
*        Then move data from importable table to current table
* Parameters:  importdir = location of table to import
*              filename  = name of current table
*              importname = name of table to import
* Variables:   imp = number of fields in import table
*              cur = number of fields in current table
*              afieldname = field name from array
*              afieldtype = field type from array
*              afieldsize = field size from array
*              zfieldtoadd = combination of data to add using ALTER TABLE
*              zexecute = code to execute to import data from import table
*              aMyStruc = array containing structure of current TABLE
*===============================================================
LPARAMETERS importdir, curtable, oldtable
LOCAL old,cur,afieldname,afieldtype,afieldsize,zfieldtoadd,impadd
LOCAL zoldtable,zcurtable,impadd
***********************************
** determine whether number of fields in importfile = number of fields 
** in currentfile if current table is larger than import table, add fields 
** to import table from current table if import table is larger than current
** table, warn user and exit if fields in importfile and currentfile are the
** same are, just import the data.

impadd = importdir + oldtable + '.DBF'
IF ! FILE(impadd)  && If prior code doesn't find tables to be imported
    THIS.omessage.INFORM('Table ' + impadd + ' does not exits. ' + ;
         'Continuing to import other tables.')
    RETURN
ELSE
    IF ! USED(curtable)
        SELECT 0
        USE (curtable) SHARED
    ENDIF

    IF ! USED(oldtable)
        SELECT 0
        USE (impadd) SHARED
    ENDIF

    old = FCOUNT(oldtable)
    cur = FCOUNT(curtable)
    zoldtable = "'" + oldtable + "'"
    zcurtable = "'" + curtable + "'"

    DO CASE
        CASE old < cur
            ** Remove records from current table
            IF USED(curtable)
                SELECT (curtable)
                USE
            ENDIF
            SET SAFETY OFF
            USE (curtable) IN 0 EXCLUSIVE
            SELECT (curtable)
            IF THIS.Zappend = .F.
                ZAP
            ENDIF USED(curtable)
            SET SAFETY ON
            ** Open old table exclusively for adding fields
            IF USED(oldtable)
                SELECT (oldtable)
                USE
            ENDIF USED(oldtable)
            SELECT 0
            USE (impadd) EXCLUSIVE

            SELECT (curtable)
            = AFIELDS(aMyStruc, (curtable))  && structure array 
            DO WHILE old < cur
                old = old + 1
                afieldname  = aMyStruc(old,1)
                afieldtype  = aMyStruc(old,2)
                afieldsize  = aMyStruc(old,3)
                afielddeci  = aMyStruc(old,4)
                IF afielddeci # 0
                    zfieldtoadd = afieldname + [ ] + afieldtype + [(] + ;
                    TRANSFORM(afieldsize) + [,]+TRANSFORM(afielddeci) + [)]
                ELSE
                    zfieldtoadd = afieldname + [ ] + afieldtype + [(] + ;
                    TRANSFORM(afieldsize) + [)]
                ENDIF
                SELECT (oldtable)
                ALTER TABLE (oldtable) ADD COLUMN &zfieldtoadd
            ENDDO
            SELECT (curtable)
            INSERT INTO (curtable) SELECT * FROM (oldtable)

        CASE old > cur
            ** Notify user number of import fields exceeds number of fields 
            ** in current table and the process can not continue.
            THISFORM.omessage.INFORM('The number of import fields ' + ;
            'exceeds number of fields in '+ curtable +'. Import failed.' + ;
            ' Data may have been corrupted. Import backup.')
            RETURN
        CASE old = cur
            ** Removes records from current table then imports data from 
            ** import table to current table
            IF USED(curtable)
                SELECT (curtable)
                USE
            ENDIF
            SET SAFETY OFF
            USE (curtable) IN 0 EXCLUSIVE
            SELECT (curtable)
            IF THIS.Zappend = .F.
                ZAP
            ENDIF USED(curtable)
            SET SAFETY ON

            SELECT (curtable)
            INSERT INTO (curtable) SELECT * FROM (oldtable)
    ENDCASE
    THIS.cmdatamanager.updateall((curtable))
    SELECT (curtable)
    USE
    USE (curtable) IN 0 SHARED
    SELECT (oldtable)
    USE
ENDIF
Previous
Reply
Map
View

Click here to load this message in the networking platform