Skip to content
Advertisement

Javascript print SQL first item in ResultSet without comma, then all others with comma

create procedure my_stored_procedure(TABLE VARCHAR)
  returns varchar
  language javascript
  as
    $$

var sql_col_list = `select distinct key_name from TABLE;`
results = snowflake.execute({sqlText: sql_col_list});

while (results.next())
{
     script += ',' + results.getColumnValue(1) + 'n';
}

Current output:

, column1
, column2
, column3

Desired output:

  column1
, column2
, column2

How can I do this in javascript within the sql stored procedure? I would like to not have to execute the query more than once because the actual query is computationally intensive.

Advertisement

Answer

you can add boolean check if this first or not:

var sql_col_list = `select distinct key_name from TABLE;`
results = snowflake.execute({sqlText: sql_col_list});

var isFirst = true;

while (results.next())
{
      if(isFirst)
      {
        script += results.getColumnValue(1) + 'n';
        isFirst = false;
        continue;
      }
     script += ',' + results.getColumnValue(1) + 'n';
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement