问题
I am working with stored procedures in Snowflake. I want to know how to safely check that there are columns in a resultSet before running getColumnValue() which errors if I try to call it on a non-existent column. If I run this
var query = `SELECT * FROM somewhere`
var result = snowflake.execute({sqlText: query});
var count = result.getColumnCount();
I get an error saying that getColumnCount is not a function. If I run
var query = `SELECT * FROM somewhere`
var result = snowflake.execute({sqlText: query});
result.next();
var count = result.getColumnCount();
I get the same error.
EDIT: I took some advice and tried
var query = `SELECT * FROM somewhere`
var stmt = snowflake.createStatement({sqlText: query});
var result = stmt.execute();
var colCount = stmt.getColumnCount();
but I sometimes get an error saying ResultSet is empty or not prepared, call next() first
so I tried
var query = `SELECT * FROM somewhere`
var stmt = snowflake.createStatement({sqlText: query});
var result = stmt.execute();
result.next()
var colCount = stmt.getColumnCount();
but I get the same error. I am assuming in those cases ResultSet might be empty but I would have thought colCount would == 0.
So either the developers haven't implemented a way to get this ahead of time or it isn't documented or I am missing something (the most likely).
Does anyone know of a way to check how many columns are in a row of a result set (or check that a result set has any columns at all) in Snowflake without throwing an error?
回答1:
getColumnCount()
is a method of a Statement, not of a ResultSet.
Instead of:
var query = `SELECT * FROM somewhere`
var result = snowflake.execute({sqlText: query});
var count = result.getColumnCount();
Do:
var query = `SELECT * FROM somewhere`
var stmt = snowflake.createStatement({sqlText: query});
var result = stmt.execute();
var col_count = stmt.getColumnCount();
来源:https://stackoverflow.com/questions/64778610/in-a-snowflake-stored-procedures-is-there-a-way-to-check-how-many-columns-are-i