4

somewhat related to another snowflake question I opened In a Snowflake stored procedures, is there a way to check how many columns are in a resultSet?

Is there a way to check if a resultset is empty? For example

var query = `SELECT * FROM somewhere`
var stmt = snowflake.createStatement({sqlText: query});
var result = stmt.execute();
result.next()
var colCount = stmt.getColumnCount();

errors if there was no data returned in the resultset. It says

Failed Code 100183 State P0000 Message ResultSet is empty or not prepared, call next() first

I tried flipping it just to see what would happen.

var query = `SELECT * FROM somewhere`
var stmt = snowflake.createStatement({sqlText: query});
var result = stmt.execute(); 
var colCount = stmt.getColumnCount();
result.next();

It looks like both of these lines var colCount = stmt.getColumnCount(); and result.next(); throw the same error regardless of order. I suspect that the error is because result set is empty. If my suspicion is correct then I need another way to check that. Is there a safe way to check that there is any data in result at all before this error gets thrown?

aaron
  • 537
  • 2
  • 6
  • 17

1 Answers1

3

You can get the count of a resultset this way:

statement.getRowCount();

In your code, you would do this:

var query = `SELECT * FROM somewhere`
var stmt = snowflake.createStatement({sqlText: query});
var result = stmt.execute();
if (stmt.getRowCount() > 0) {
    result.next()
    var colCount = stmt.getColumnCount();
} else {
    // Do something here if no rows in the resultset.
}
Greg Pavlik
  • 10,089
  • 2
  • 12
  • 29