cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
cancel
Showing results for 
Search instead for 
Did you mean: 

Running SQL Data File through Notebook Python

Marco9898
New Contributor II

I am attempting to run larger sql scripts through Databricks Notbook and export data to a file. For the most part the Notebook works when the sql script is a single SELECT statement. However, if the sql file is more complicated such as involving the use of creating a temporary/volatile table, inserting data to it, updating it, and then exporting the results it fails as shown below.

I am able to run the SQL script without any issues on Snowflake, but when establishing a connection though Databricks it just doesn't want to run and I do not understand the issue.

%python
 
#Full SQL script runs on Snowflake, but seems to have issues here
 
with open('/dbfs/FileStore/myid/prjX_sf.sql', 'r') as sqlFile:  #ERROR, create temp table, insert data, update temp table, select statement
 
#with open('/dbfs/FileStore/myid/prjX_simple.sql', 'r') as sqlFile: #WORKS, select statement only
 
  sql_script = sqlFile.read()
 
 
#Read data from Snowflake table
df = spark.read \
 .format("snowflake") \
 .options(**conn) \
 .option("query", sql_script) \
 .load()
display(df)

net.snowflake.client.jdbc.SnowflakeSQLException: SQL compilation error:

Py4JJavaError: An error occurred while calling o3645.load.

: net.snowflake.client.jdbc.SnowflakeSQLException: SQL compilation error:

syntax error line 1 at position 15 unexpected 'DROP'.

syntax error line 285 at position 63 unexpected ')'.

2 REPLIES 2

jose_gonzalez
Moderator
Moderator

Hi @Marco Perez​,

I would like to share the following doc https://docs.databricks.com/external-data/snowflake.html#query-a-snowflake-table-in-databricks in here you will be able to find example code on how to read data using databricks

Anonymous
Not applicable

Hi @Marco Perez​ 

Does @Jose Gonzalez​ response answer your question? If yes, would you be happy to mark it as best so that other members can find the solution more quickly?

We'd love to hear from you.

Thanks!

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.