Wednesday, November 18, 2015

Batch Insert example in SAP HANA


The following example of usage of the SAP HANA XS database API shows how to establish a connection with SAP HANA and how to use batch insert for loading bulk data into hana DB.

The example values assumes that we have to insert data into DB as a batch insert instead by record by record.

COUNTRYID
COUNTRYNAME
1
India
2
USA
3
UK


Syntax

HANA 

/////if you have sqlcc file please mentioned in the getConnection method
  
     var conn = $.db.getConnection();
     var st = conn.prepareStatement("insert into tablename(col1,col2) VALUES(?,?)");
     var BACTHSIZE = number of records which you want insert;

   ///////we can use batch only number of records are more than 1

     if(BACTHSIZE>1)
     {
     st.setBatchSize(BACTHSIZE);
     var i;
     for(i=0;i<BACTHSIZE;i++) 
     {
     st.setString(1,input[i].COUNTRYID);
     st.setString(2,input[i].COUNTRYNAME]);
    
     st.addBatch();
     }
     st.executeBatch();
     }

////if number of records are less than 1, we can assume only one record need to insert into db and i ////value should be zero
     else
     {
     st.setString(1,input[0].COUNTRYID);
     st.setString(2,input[0].COUNTRYNAME);
    
     }
     st.close();
     
     conn.commit(); 
     data.push({
        "data": "Record Inserted Successfully..!"
      });
       
      
  }

No comments:

Post a Comment