Insertar un DBF en una table de SQL

Post Reply
User avatar
mariordz
Posts: 131
Joined: Tue Dec 26, 2006 4:50 pm
Location: Ciudad de México

Insertar un DBF en una table de SQL

Post by mariordz »

Buenas tardes, he estado trabajando en exportar mis datos de DBF's a una base de datos de SQL, hasta ahora todo bien, pero me gustaría saber como agrego información que tengo en una DBF temporal a una tabla SDQL que ya existe.

He estado usando la instrucción

Code: Select all | Expand

FW_AdoImportFromDBF( oCn, "&dbtempo" )


Pero esta genera una tabla nueva, pero quiero insertar datos en una tabla que ya existe, si tienen alguna sugerencia les agradezco su ayuda.
User avatar
cnavarro
Posts: 6667
Joined: Wed Feb 15, 2012 8:25 pm
Location: España
Has thanked: 9 times
Been thanked: 12 times

Re: Insertar un DBF en una table de SQL

Post by cnavarro »

Intenta poniendo el nombre de la tabla destino en el tercer parámetro.
Cristobal Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
User avatar
mariordz
Posts: 131
Joined: Tue Dec 26, 2006 4:50 pm
Location: Ciudad de México

Re: Insertar un DBF en una table de SQL

Post by mariordz »

Code: Select all | Expand

FW_AdoImportFromDBF( oCn, "&dbtempo","&cRealdb" )


Me aparece el mensaje de que la tabla ya existe y si deseo sobre escribirla.
User avatar
cnavarro
Posts: 6667
Joined: Wed Feb 15, 2012 8:25 pm
Location: España
Has thanked: 9 times
Been thanked: 12 times

Re: Insertar un DBF en una table de SQL

Post by cnavarro »

Qué version de Fw utilizas?
Cristobal Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
User avatar
mariordz
Posts: 131
Joined: Tue Dec 26, 2006 4:50 pm
Location: Ciudad de México

Re: Insertar un DBF en una table de SQL

Post by mariordz »

FWH 19.12
Bcc7.7
Harbour 3.2.0 dev (r1603082110)

Por el momento como alternativa exporte mi DBF temporal a una tabla nueva, de esa tabla la inserté a la tabla real y después elimine la tabla temporal, posiblemente no sea el mejor método, pero por el momento me esta funcionando.
User avatar
Enrico Maria Giordano
Posts: 8796
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Has thanked: 1 time
Been thanked: 9 times
Contact:

Re: Insertar un DBF en una table de SQL

Post by Enrico Maria Giordano »

mariordz wrote:FWH 19.12
Bcc7.7
Harbour 3.2.0 dev (r1603082110)


Does exist a version 7.7 of BCC? This is the latest, as far as I know:

Code: Select all | Expand

Embarcadero C++ 7.40 for Win32 Copyright (c) 1993-2018 Embarcadero Technologies, Inc.


EMG
User avatar
nageswaragunupudi
Posts: 10733
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Been thanked: 11 times
Contact:

Re: Insertar un DBF en una table de SQL

Post by nageswaragunupudi »

Buenas tardes, he estado trabajando en exportar mis datos de DBF's a una base de datos de SQL, hasta ahora todo bien, pero me gustaría saber como agrego información que tengo en una DBF temporal a una tabla SDQL que ya existe.


1. Is the second table has the same structure as the first table?
2. Does the second table contain modifications to the existing records or additional records or both?

Depending on this information one of these methods would be useful

Insert( ... )
Upsert( .. )
AppendFromAlias(...)

We can advise you better if you clarify the above points
Regards

G. N. Rao.
Hyderabad, India
User avatar
mariordz
Posts: 131
Joined: Tue Dec 26, 2006 4:50 pm
Location: Ciudad de México

Re: Insertar un DBF en una table de SQL

Post by mariordz »

Thanks MR. Rao, both tables have the very same structure, but both have a ID column generated when I imported the data from my old DBF's, therefore I cannot just insert one table into another because I got an error, instead of that I generate one temporal DBF, export it to a table and the copy the fields, except the one with the ID, I know it sound much more complicated that it shuld be, but it was the workaround I found.

To acomplish that I use the next functions:

First I generate the tempral DBF to be send to SQL:

Code: Select all | Expand

copy to &dbtmp7 fields field1,field2,field3,...


then I create a variable containing the fields I want to add to the real database (I do this because when exporting the DBF to SQL the table will be created with the ID field automatically)

Code: Select all | Expand

cField:="field2,field2,field3,...."


Then I call my function that inserts the DBF to SQL, adds just the fields that I need to insert into the real table an then drops the temporal table, passing 3 parameters, the temporal DBF, the real SQL table and the list of fields to be copied

Code: Select all | Expand

tempo2sql(dbtmp7,tRepos,cField)


And this is the Function that creates, inserts and deletes the temporal table (quite complicated, isn't it?)

Code: Select all | Expand

Function tempo2sql(tempdb,realdb,cField0)
    local oCn
    cTemporal:=tempdb
    cRealdb:=realdb
    cFields=cField0
    cTabtem:=cFilename(cTemporal)
   close all
   dbtempo:=alltrim(cTemporal)+".dbf"
   oCn   := FW_OpenAdoConnection( { "MSSQL", xSOURCE, xCATALOGA, xUSERID, xPASSWORD }, .t. )
   if oCn == nil
      ? "Failed to connect"
      return .f.
   endif
   
   if FW_AdoImportFromDBF( oCn, "&dbtempo" )
      lImported=.T.
   else
      ? "Import Fail"
      lImported:=.F.
   endif
   oCn:Close()

    if lImported=.T.
        cCadsql0:="insert into &cRealdb select &cFields from &cTabtem"
       
        oRs0 := TOleAuto():New( "ADODB.Recordset" )
        oRs0:CursorType := 1 // opendkeyset
        oRs0:CursorLocation := 3 // local cache
        oRs0:LockType := 3 // lockoportunistic
   
        TRY      
            cursorwait()
            oRS0:Open( cCadSql0,'Provider='+xPROVIDER+';Data Source='+xSOURCE+';Initial Catalog='+xCATALOGA+';User Id='+xUSERID+';Password='+xPASSWORD )

            CATCH oErr
            MsgInfo( "Error inserting data to the real SQL table" )
            RETURN(.F.)
        END TRY
       
       
        cCadsql01:="drop table &cTabtem"
        oRs01 := TOleAuto():New( "ADODB.Recordset" )
        oRs01:CursorType := 1 // opendkeyset
        oRs01:CursorLocation := 3 // local cache
        oRs01:LockType := 3 // lockoportunistic
   
        TRY      
            cursorwait()
            oRS01:Open( cCadSql01,'Provider='+xPROVIDER+';Data Source='+xSOURCE+';Initial Catalog='+xCATALOGA+';User Id='+xUSERID+';Password='+xPASSWORD )

            CATCH oErr
            MsgInfo( "Error dropping temporal table" )
            RETURN(.F.)
        END TRY
    endif
return nil


Eventhough it is working , I know there has to be a simpler way to do this.
Post Reply