JDD/Methods/Bulk Insert

From J Wiki
Jump to navigation Jump to search
JDD: Driver Locale | Handles | Data Sources | Data Driver | Error Messages | API: Connect Read Update Create Bulk Insert

To insert more than one record, you can use either of the following 3 methods. Here we insert some random records into tdata. Note that data are in a columnar form similar to that returned by ddfch.

  len=. 1e3       NB. insert 1000 rows
  data=. ((len,5)$'A''BCDEF');((len,1)$'MF');((len,4)$'E101E201');((len,10)$'1991-02-13');((len,10)$'2008-12-03');(,. 1+i.len)


A select statement is executed to obtain a cursor, but actually no record will be retrieved.

  r=. ch ddins__db~ 'select NAME, SEX, DEPT, DOB, DOH, SALARY from tdata';data

Finally, we read the record again, to ensure the update was successful:

    smoutput 'number of records inserted', ":ddcnt__db ch
    sh=. ch ddsel__db~ 'select * from tdata where DOH=''2008-12-03''' do.
    smoutput ddfet__db sh,5
    ddend__db sh


Parameterized Query

A parameterized query is a query that contains some unknown values when the statement is prepared, For example

   sql=. 'insert into tdata(NAME, SEX, DEPT, DOB, DOH, SALARY) values (?,?,?,?,?,?)'

The RDBMS engine can not infer types of input parameters, so their types have to provided by client applications.


Types (and optionally lengths) together data are appended to SQL statement when using ddparm. For example

  r=. ch ddparm__db~ sql;((3#SQL_VARCHAR),(2#SQL_TYPE_DATE),SQL_INTEGER);data

ODBC data types are used here although JDD drivers do not use ODBC.


This is a smarter version of ddparm in that data types will be guessed by JDD drivers, so data types are not needed. This is applicable to queries on a single table only. If it fails, your should try ddparm instead.

  r=. ch ddsparm__db~ sql;data
  • speed of bulk insert depends very much on capacity of individual RDBMS. Some are very fast while others are very slow.
  • ddparm and ddsparm also support update and delete query.
  • ddparm and ddsparm do not support queries that return records, eg. select statement.