NB. Excel ODBC examples (Excel 97)
NB.
NB. See odbc.txt for general comments on these scripts
NB.
NB. requires ODBC setup of:
NB.   data source name:  jexcel
NB.   data source:       system\examples\data\jdata.xls
NB.
NB. The Excel driver connects to an Excel file. ODBC tables
NB. correspond to workbooks in the file.
NB.
NB. The specific workbook accessed is tdata. Note that Excel
NB. refers to it as tdata$, and that references may need to
NB. be enclosed in double quotes, as in "tdata$".
NB.
NB. verbs:
NB.   xlconnect    connect to excel driver
NB.   xlcolumns    columns of tdata
NB.   xlreadsel    read a selection from tdata
NB.   xlupdate     update a field

require 'dd'

demo=: 3 : 0
out=. 1!:2 & 2
empty xlconnect''
out 'columns:',": xlcolumns''
out 'read all:',": xlreadall''
out 'read selection:', ": xlreadsel''
empty xlupdate''
out 'read updated selection:', ": xlreadsel''
empty dddis CH
)

NB. =====================================================
NB. xlconnect
NB. connect to excel driver,
NB. define and return connection handle CH
xlconnect=: 3 : 0
CH=: ddcheck ddcon 'dsn=jexcel'
)

NB. =====================================================
NB. xlcolumns
NB. read columns of tdata
xlcolumns=: 3 : 0
ddcheck 'tdata$' ddcol CH
)

NB. =====================================================
NB. xlreadall
NB. read all tdata
xlreadall=: 3 : 0
sel=. 'select * from "tdata$"'
sh=. ddcheck sel ddsel CH
if. #sh do.
  r=. ddfch sh,_1
  ddend sh
  r
end.
)

NB. =====================================================
NB. xlreadsel
NB. read a selection from tdata
xlreadsel=: 3 : 0
sel=. 'select * from "tdata$" where sex=''M'' and salary >= 40000'
sh=. ddcheck sel ddsel CH
if. #sh do.
  r=. ddfch sh,_1
  ddend sh
  r
end.
)

NB. =====================================================
NB. xlupdate
NB. update salary field for Abbott K (increment by 1)
xlupdate=: 3 : 0
sel=. 'select * from "tdata$" where name=''Abbott K'''
sh=. ddcheck sel ddsel CH
if. #sh do.
  newsal=. >: {. , >{:,ddfch sh
  usel=. 'update "tdata$" set salary=',(":newsal),' where name=''Abbott K'''
  ddcheck usel ddsql CH
end.
)
