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: 

Select from a dynamic table name returned by databricks function

sgupta
New Contributor II
  • I have a databricks function that returns a table_name
CREATE OR REPLACE FUNCTION test_func()
  RETURNS string
  READS SQL DATA
  RETURN    'table_name'
  • I want to select from the table that is returned by this function. How can I make it work in SQL, something like this -

SELECT * FROM (SELECT test_func())

Currently this SELECT is returning a single row with the table name and not the contents of the table name.

2 REPLIES 2

sgupta
New Contributor II

Thanks @Retired_mod . This does not work in Databricks notebook, error attached. This looks like a SQL Server solution. 

sgupta
New Contributor II

I looked at this post

https://stackoverflow.com/questions/77475436/in-databricks-workbook-using-spark-sql-how-to-pass-para...

 What I want is to replace the static table name with the table name passed as parameter (param_table_name). Is it possible?

 

CREATE OR REPLACE FUNCTION mydb.tbl_filter(a_val INT, param_table_name string)
RETURNS TABLE(a INT, b INT)
RETURN SELECT * FROM tbl_filter.param_table_name tf 
WHERE tf.a = tbl_filter.a_val;

 

 

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