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

478
Visualizações
How to dynamically select columns from a given table in snowflake stored procedure?

I am trying to create a procedure that choose columns dynamically from two table. I have already hardcoded query which works fine.

#Existing query

create table mydb.result_table as 
select 
t1.id,
t1.name,
t1.place,
t1.product
t1.prize
t2.id,
t2.name,
t2.place,
t2.product,
t2.prize
from source_db.source_table t1
full outer join target_db_target_table t2 on 
t1.id=t2.id and t1.name=t2.name
where t1.place<>t2.place

Now i need to create a procedure which basically select column dynamically from given table name and join both with given keys

so something like below

CREATE OR REPLACE PROCEDURE result(source_db varchar, source_table VARCHAR, targt_db varchar, target_table VARCHAR, key_join varchar<not sure can pass list>, filter_col varchar )
returns string not null
language javascript
as
$$
var query= `create table mydb.result_table as 
select
t1.<all columns from source_db and source_table>
t2.<all columns from targt_db and target_table >
from <source_db and source_table> as t1
full outer join  <target_db_target_table> as t2
on <key_join> where <t1.filter_col <> t2.filter_col>
`
return 'success';
$$;

i don't see example of selecting columns dynamically for this kind of use case. Any solution to this?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You need to query the information schema for the list of columns in each table and use the result to dynamically build your SQL statement.

You also need to be aware of the same column name existing in multiple tables as you obviously can’t have the same column appearing multiple times in a table - so you’ll need to dynamically rename columns as appropriate

about 4 years ago · Juan Pablo Isaza Relatório

0

Just query the INFORMATION_SCHEMA.COLUMNS view to access the table's columns and then you can build your query dynamically:

https://docs.snowflake.com/en/sql-reference/info-schema/columns.html

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