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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing Data Types 2

Status
Not open for further replies.

smartglass

IS-IT--Management
Mar 14, 2006
33
GB
Hi there:
I am trying to bring together 2 completely disparate databases into one. The data is essentially similar but the data types are different; thus 'order number' is an integer in one and text in another.
I am pulling the data from one into a paradox table, ready for import into the other.
Is there any way of programatically changing the data type in the paradox table so as to allow import into the other?
i have thought about exporting to a text file, then bringing into a new table in the propoer format but is there a better way?
Thanks in advance!
 
How about opening a tcursor on each table, the scanning the one table, inserting a record into the other and assigning the values from the one cast to the proper data type into the other.

Code:
tcS.open("sourcetable.db")
tcD.open("destination.db")
tcD.edit()
scan tcS :
  tcD.insertafterrecord()
  tcD."fieldone"=string(tcS."longintfield")
  try
    tcD.unlockrecord()
  onFail
  ; handle errors
  endtry
endscan
tcD.endedit()
tcD.close()
tcS.close()

Tony McGuire
"It's not about having enough time; we have the rest of our lives. It's about priorities.
 
smartglass,

A way that I would approach it is to get one table to have the same data types as the other by actually programmatically restructuring the data types of the fields. For example you may wish to either look for text fields and change them to numbers or look for specific named fields and change them to numbers.

I've put a bit of code below and tried to comment it. Hopefully you get what I am trying to do. If you get as far as enumerating the field structure then view it and all will become clear. You will need to substitute your own names where necessary and ensure that no one is using the table etc.

Please let me know if you have any questions.

Regards

Bystander


method prep()

Var
tcFlds TCursor
tbl Table
dynNewStru DynArray[] Anytype
EndVar

;=================================================
;Attach your table that you want to change
;and get its field structure
;=================================================

tbl.attach( ":pRIV:your_source_table.DB" )
tbl.enumFieldStruct(":priv:__field_struct.db")


;==================================================
;open a TCursor to read through the field sturcture
;==================================================

tcFlds.open(":priv:__field_struct.db" )
tcFlds.edit()


;======================================================
;scan through the field sturcture
;either look for specific fields or field types
;both examples are below and make your changes
;======================================================

Scan tcFlds:

if tcFlds."Field Name"= "XXXXX" then
tcFlds."Type"= "Number"
tcFlds."Size"=1
tcFlds."Dec"=0
Endif
EndScan

Scan tcFlds:
if tcFlds."Type"= "Alpha" then
tcFlds."Type"= "Number"
tcFlds."Size"=1
tcFlds."Dec"=0
Endif
EndScan

tcFlds.endEdit()
tcFlds.close()

;====================================================
;replace your source table's field sturcture with the
;revised one
;=====================================================


dynNewStru["FIELDSTRUCT"] = ":priv:__field_struct.db"
tbl.restructure( dynNewStru )

endmethod
 
Thanks to you both for your help.
I have applied both methods - and copying the whole lot out to text and back again.
In the end I have gone foe Bystanders approach; I need two passes to convert a memo field to anumber but it works just fine.
Thanks again.
 
smartglass,

Good point, I forgot to mention about type casting. Glad you sorted it out and glad to have been of service.

Regards

Bystander
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top