Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

85
Visualizações
Stored Procedure Snowflake with IF statement Null Check

I edited the previous code to make it simpler. Below returns to me always SELECT 2. I expect to have it SELECT 1 because etlId is 0.

Where could the problem be?

Thanks

Regards

'
function getScalarValue(queryString) {
    var out;
    cmd = {sqlText: "set etl_id = 0 "};
    stmt = snowflake.createStatement(cmd);
    var rs;
    rs = stmt.execute();
    rs.next();
    return rs.getColumnValue(1);
    return out;
}
  var etlId = getScalarValue("select $etl_id");
        if (etlId == 0 )  
                {
                    var sql_command = "SELECT 1";
                }
        else {
                    var sql_command = "SELECT 2";
                  }
            try {
            snowflake.execute (
                {sqlText: sql_command}
                );
            return "Success" ;   // Return a success/error indicator.
            }
        catch (err)  {
            return "Failed: " + err;   // Return a success/error indicator.
            }                   
'
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

$etl_id is not a JavaScript variable. It is a SQL variable. You can use a helper function get scalar values from a SQL statement. This helper function gets the first column, first row value and returns it:

Update: Based on your updates, it's more like this:

create or replace procedure TEST()
returns string
language javascript
execute as caller
as
$$

  var etlId = getScalarValue("select $etl_id");
        if (etlId == 0 )  
                {
                    var sql_command = "SELECT 1";
                }
        else {
                    var sql_command = "SELECT 2";
                  }
            try {
            snowflake.execute (
                {sqlText: sql_command}
                );
            return "Success" ;   // Return a success/error indicator.
            }
        catch (err)  {
            return "Failed: " + err;   // Return a success/error indicator.
            }  
            
function getScalarValue(queryString) {
    var out;
    cmd = {sqlText: queryString};
    stmt = snowflake.createStatement(cmd);
    var rs;
    rs = stmt.execute();
    rs.next();
    return rs.getColumnValue(1);
    return out;
}

$$;

set etl_id = 1;
call test();
about 4 years ago · Juan Pablo Isaza Relatório

0

Thanks for your reply. I have another solution to use a variable as a parameter at the script. When I call the procedure, I can use SQL to define the parameter so this works for me. Once again thank you very much for your help.

Regards

       create or replace procedure TEST(etlId, float)
        returns string
        language javascript
        execute as caller
        as
        $$
                if (etlId == 0 )  
                        {
                            var sql_command = "SELECT 1";
                        }
                else {
                            var sql_command = "SELECT 2";
                          }
                    try {
                    snowflake.execute (
                        {sqlText: sql_command}
                        );
                    return "Success" ;   // Return a success/error indicator.
                    }
                catch (err)  {
                    return "Failed: " + err;   // Return a success/error indicator.
                    }  
      $$
    
call TEST (select count(etl_id) etlId from view where etl_id not in (select etl_id from table))
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda