Adhi cloud
Adhi cloud

Reputation: 49

How to create view from list of columns names in Snowflake?

I am trying to create a view from a list of columns from my Python code.

I found many way to create a view by specifying each colum as follows

"create view myview as select col1, col2 from mytable;"

but here i need to use a list of columns to create the view as follows

col_list=['col1','col2','col3']


query="""create view my_view as select {*col_list}from @mystage"""

Is this the right way to create a view from a list of columns?

Upvotes: 2

Views: 329

Answers (1)

Lukasz Szozda
Lukasz Szozda

Reputation: 175706

Using string interpolation:

col_list=['col1','col2','col3']

query="""create view my_view as select {} 
from @mystage""".format(', '.join(str(x) for x in col_list))

print(query)

Output:

create view my_view as select col1, col2, col3

from @mystage

Warning:

Building SQL string could lead to SQL Injection if the input is not trusted/validated.

Upvotes: 2

Related Questions