FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour Insertar un DBF en una table de SQL
Posts: 131
Joined: Tue Dec 26, 2006 04:50 PM
Insertar un DBF en una table de SQL
Posted: Mon Feb 24, 2020 07:47 PM
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 (fw): Select all Collapse
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.
Posts: 6755
Joined: Wed Feb 15, 2012 08:25 PM
Re: Insertar un DBF en una table de SQL
Posted: Mon Feb 24, 2020 07:59 PM

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
Posts: 131
Joined: Tue Dec 26, 2006 04:50 PM
Re: Insertar un DBF en una table de SQL
Posted: Mon Feb 24, 2020 09:25 PM
Code (fw): Select all Collapse
FW_AdoImportFromDBF( oCn, "&dbtempo","&cRealdb" )


Me aparece el mensaje de que la tabla ya existe y si deseo sobre escribirla.
Posts: 6755
Joined: Wed Feb 15, 2012 08:25 PM
Re: Insertar un DBF en una table de SQL
Posted: Mon Feb 24, 2020 10:57 PM

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
Posts: 131
Joined: Tue Dec 26, 2006 04:50 PM
Re: Insertar un DBF en una table de SQL
Posted: Tue Feb 25, 2020 03:02 PM

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.

Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Insertar un DBF en una table de SQL
Posted: Wed Feb 26, 2020 12:38 PM
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 (fw): Select all Collapse
Embarcadero C++ 7.40 for Win32 Copyright (c) 1993-2018 Embarcadero Technologies, Inc.


EMG
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Insertar un DBF en una table de SQL
Posted: Wed Feb 26, 2020 04:21 PM
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
Posts: 131
Joined: Tue Dec 26, 2006 04:50 PM
Re: Insertar un DBF en una table de SQL
Posted: Tue Mar 03, 2020 03:50 PM
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 (fw): Select all Collapse
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 (fw): Select all Collapse
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 (fw): Select all Collapse
tempo2sql(dbtmp7,tRepos,cField)


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

Code (fw): Select all Collapse
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.

Continue the discussion