Connecting to SQL Server from Databricks

To connect to SQL Server from Databricks, you can use the included driver in Databricks Runtime 11.3 LTS and above. Here’s how you can do it:

When working with DataFrames, use the following syntax:

      remote_table = (spark.read
        .format("sqlserver")
        .option("host", "hostName")
        .option("port", "port")  # optional, can use default port 1433 if omitted
        .option("user", "username")
        .option("password", "password")
        .option("database", "databaseName")
        .option("dbtable", "schemaName.tableName")  # (if schemaName not provided, default to "dbo")
        .load())
    

Alternatively, for SQL queries, specify sqlserver in the USING clause:

      DROP TABLE IF EXISTS sqlserver_table;
      CREATE TABLE sqlserver_table
      USING sqlserver
      OPTIONS (
        dbtable '',
        host '',
        port '1433',
        database '',
        user '',
        password ''
      );
    

For Databricks Runtime 10.4 LTS and below, you must use the JDBC driver:

      driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
      database_host = ""
      database_port = "1433"  # update if you use a non-default port
      database_name = ""
      table = ""
      user = ""
      password = ""
      url = f"jdbc:sqlserver://{database_host}:{database_port};database={database_name}"
      remote_table = (spark.read
        .format("jdbc")
        .option("driver", driver)
        .option("url", url)
        .option("dbtable", table)
        .option("user", user)
        .option("password", password)
        .load())
    

Frequently Asked Questions

Bottom Line: Connecting to SQL Server from Databricks is straightforward using the included driver in newer runtime versions or the JDBC driver in older versions. However, for full query federation and data governance features, consider using Lakehouse Federation.


👉 Hop on a short call to discover how Fog Solutions helps navigate your sea of data and lights a clear path to grow your business.