Skip to content

Instantly share code, notes, and snippets.

@jbratu
Created May 23, 2016 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbratu/8ad8dbd304e20ed6e893f3256da17bc3 to your computer and use it in GitHub Desktop.
Save jbratu/8ad8dbd304e20ed6e893f3256da17bc3 to your computer and use it in GitHub Desktop.
A basic copy table function.Example routine creates the destination dictionary and data tables and writes the data using a select/readnext/write loop.
Subroutine CS_SNIP_CreateCopy_ReadNext(void)
Declare Subroutine Set_Status, create_Table,Msg,attach_table
declare function Set_FSError,Msg
$Insert Logical
*Source table to copy from
TABLE_NAME = "CUSTOMERS"
*Destination table to create and copy to
TABLE_NAME_DEST = TABLE_NAME : '_' : Date() : '_' : Time()
*Location for destination table
*DEST_VOL = "DATAVOL"
DEST_VOL = 'C:\Revsoft\temp'
err = ''
*
* Create the data portion
*
CreateDict = False$
AttribList = "1000,400,25,1024,80"
DestTableApp = '' ;*Own by current app
Convert "," to @FM in AttribList
Set_Status(0)
Create_Table(DEST_VOL, TABLE_NAME_DEST, CreateDict, DestTableApp, AttribList, False$)
If Get_Status(err) Then
Msg('Create Table ' : TABLE_NAME_DEST : ' Error:|' : err)
Return
End
*
* Create the dictionary
*
CreateDict = True$
AttribList = "1000,400,25,1024,80"
Set_Status(0)
Create_Table(DEST_VOL, "DICT." : TABLE_NAME_DEST, CreateDict, DestTableApp, AttribList, False$)
If Get_Status(err) Then
Msg('Create Table DICT.' : TABLE_NAME_DEST : ' Error:|' : err)
Return
End
*
* Attach the new table
*
Set_Status(0)
Attach_table(DEST_VOL,TABLE_NAME_DEST, DestTableApp,'')
If Get_Status(err) Then
Msg('Unable to attach ' : TABLE_NAME_DEST : ' from ' : DEST_VOL : ' error:|' : err)
Return
End
*
* Open source and destination tables
*
open TABLE_NAME To TABLE else
status = Set_FSError()
Msg('Open Table ' : TABLE_NAME : ' error:|' : status)
return
End
open TABLE_NAME_DEST To TABLE_DEST else
status = Set_FSError()
Msg('Open Table ' : TABLE_NAME_DEST : ' error:|' : status)
return
end
select TABLE
Done = False$
Loop
ReadNext @ID else Done = True$
Until Done Do
read @RECORD From TABLE, @ID Then
Gosub processRecord
Write @RECORD To TABLE_DEST, @ID Else
status = Set_FSError()
Msg('Write error ' : TABLE_NAME_DEST : ' error:|' : status)
return
End
End Else
status = Set_FSError()
Msg('read error ' : TABLE_NAME : ' error:|' : status)
return
End
Repeat
return
*
* End Main
*
processRecord:
//Add your processing to @RECORD here
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment