Skip to content

Instantly share code, notes, and snippets.

@js1972
Created November 7, 2013 06:50
Show Gist options
  • Save js1972/7350169 to your computer and use it in GitHub Desktop.
Save js1972/7350169 to your computer and use it in GitHub Desktop.
Node program to execute SQL query on HANA DB #HANA #JavaScript #Node
/*
Use the HANA Node.js client library to execute a HANA SQL command.
See http://scn.sap.com/docs/DOC-48721?utm_campaign=CRM-XM13-SOC-TW_SCNL &
https://github.com/sap/node-hdb for details.
Example usage:
C:\MyScratchFolder\nodejs>node hana_sql_server.js <username> <password> <schema> "select * from t_persons"
Parameters:
Username: DEV_2HFRW10E47LHK8QDAQ4EMB5QA
Password: *****
Schema: NEO_AGQWMJSR099P2T2FX3PNSITM1
SQL: select * from t_persons
Results:
[ { ID: '1f274c36-ec32-48e5-a311-cd6cedf8b815',
FIRSTNAME: 'Jason',
LASTNAME: 'Scott' },
{ ID: '532e10e3-7c65-4970-a92a-6515bbec3bfb',
FIRSTNAME: 'Test',
LASTNAME: 'Tester' },
{ ID: '69d98631-0daa-4583-903f-960cb5431817',
FIRSTNAME: 'John',
LASTNAME: 'Doe' },
{ ID: '8d65cd02-aefc-4dbb-b654-339d88bcbba0',
FIRSTNAME: 'Jane',
LASTNAME: 'Doe' } ]
*/
// check arguments
if (process.argv[2] === undefined) {
console.log('Enter username as parameter.');
process.exit();
}
if (process.argv[3] === undefined) {
console.log('Enter password parameter.');
process.exit();
}
if (process.argv[4] === undefined) {
console.log('Enter schema.');
process.exit();
}
if (process.argv[5] === undefined) {
console.log('Enter sql as parameter surrounds by double-quotes.');
process.exit();
}
var user = process.argv[2];
var pass = process.argv[3];
var schema = process.argv[4];
var sql = process.argv[5];
// display provided arguments
console.log("\nParameters:\nUsername: " + process.argv[2] + "\nPassword: *****" + "\nSchema: " + process.argv[4] + "\nSQL: " + process.argv[5]);
// connect to HANA DB
var hdb = require('hdb');
var client = hdb.createClient({
host : 'localhost',
port : 30015,
user : user,
password : pass
}).connect();
client.exec('SET SCHEMA ' + schema, function(err, rows) {
if (err) {
console.error('Error:', err);
}
});
client.exec(sql, function(err, rows) {
if (err) {
console.error('Error:', err);
} else {
console.log('\n\nResults:\n\n', rows);
}
client.end();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment