Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

650
Vistas
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.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

using your sp, i would remove the first result from the loop so it is:

create or replace 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});
var script = '';

results.next()
script += results.getColumnValue(1)

while (results.next())
{
       script += ',' + results.getColumnValue(1) + '\n';
}
return script;
$$;
about 4 years ago · Juan Pablo Isaza Denunciar

0

A poor solve it after the fact solution, remove it:

> ',a,b,c'.replace(/^,/,'');
'a,b,c'
>

another option is to build an array of the wanted parts, and then use join

> ['a','b','c'].join(',\n');
'a,\nb,\nc'
>

and if you want the trailing newline:

> ['a','b','c'].join(',\n')+'\n';
'a,\nb,\nc\n'
>

OR you could get Snowflake to do it for you:

SELECT listagg( DISTINCT key_name, ',\\n') || '\\n'
FROM VALUES
   ('apple'),('cat'),('apple'),('cat'),('dog') t(key_name);
LISTAGG( DISTINCT KEY_NAME, ',\N')|| '\N'
apple,\ncat,\ndog\n
about 4 years ago · Juan Pablo Isaza Denunciar

0

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';
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda