cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

Notebook's Widget parameters in SQL cell => howto

yopbibo
Contributor II
dbutils.widgets.text('table', 'product')
 
%sql
select *
from ds_data.$table

Hello, the above will work.

But how can I do something like:

dbutils.widgets.text('table', 'product')
 
%sql
select *
from ds_data.$table_v3

in that example, $table is still my table name, and I want to add a suffix.

I could go in python

dbutils.widgets.text('table', 'product')
tab = dbutils.widgets.get('table')
spark.sql(f'select * from ds_data.{tab}_v3')

but would love doing it in SQL.

Any idea?

thanks!

1 ACCEPTED SOLUTION

Accepted Solutions

Prabakar
Databricks Employee
Databricks Employee

I checked further and found that the command that I used is not helpful. Though it takes the table name, it is pulling data from the agents table but not the agents_v1 table. The agents table has 12 rows and agent_v1 table has 20 rows. So this doesn't work. We need to programmatically call the table with the suffix. I don't think its possible with sql.

View solution in original post

8 REPLIES 8

Prabakar
Databricks Employee
Databricks Employee

hello @Philippe CRAVE​ you can use something like this and it should work.

%sql
select * from prabakar.`$table`_v1

image 

yopbibo
Contributor II

Thanks!

Actually, did not work for me 😞

this:

%sql
select * from prabakar.`$table`_v1

would give:

Error in SQL statement: AnalysisException: Table or view not found: prabakar.table_name;

seems like missing the "_v1" anyway

Prabakar
Databricks Employee
Databricks Employee

looks like you are using the example that I share `Table or view not found: prabakar.table_name;` Can you test with your sample data.

I tried using my sample data, translated the message to match yours

Prabakar
Databricks Employee
Databricks Employee

I checked further and found that the command that I used is not helpful. Though it takes the table name, it is pulling data from the agents table but not the agents_v1 table. The agents table has 12 rows and agent_v1 table has 20 rows. So this doesn't work. We need to programmatically call the table with the suffix. I don't think its possible with sql.

ok, thanks. Sad, forces us to move to python notebooks for something that was fitting very well a sql notebook. but that's life 🙂

thanks!

Prabakar
Databricks Employee
Databricks Employee

🙂

yopbibo
Contributor II

Maybe I should add that I use DB9.1 on a high concurrency cluster

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group