Quantcast
Channel: SCN: Message List
Viewing all articles
Browse latest Browse all 3316

Re: How to run sql statement in xsjs and get the result as json?

$
0
0

Hello Namita,

 

try following.

 

 

var query = "SELECT SUM(\"Column_Name\") FROM \"Schema_Name\".\"Table_Name\""; 
var oConnection = $.db.getConnection(); 
var oStatement = oConnection.prepareStatement(query); 
oStatement.execute(); 
var oResultSet = oStatement.getResultSet();
var result = {    records : [ ]   
}; 
while (oResultSet.next()) {   result.records.push({value: oResultSet.getInteger(1)}); 
} 
oResultSet.close(); 
oStatement.close(); 
oConnection.close(); 
$.response.contentType = "application/json; charset=UTF-8"; 
$.response.setBody(JSON.stringify(result)); 
$.response.status = $.net.http.OK; 

 

Some hints regarding your coding:

  • The JSON object you try to prepare is not valid JSON object, so JSON stringify does not produce any result.
  • Calling getString method of the result set object thems to be wrong, cause you have a numeric value in your aggregated column. So getInteger, getDouble ... depending on the type would be more appropriate, although getString works too.

 

Hope that helps you.

 

Best Regards,

Florian


Viewing all articles
Browse latest Browse all 3316

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>