<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic EXECUTE IMMEDIATE works with JDBC connection ??? in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/execute-immediate-works-with-jdbc-connection/m-p/94641#M38942</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hello, i need help&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to use the EXECUTE IMMEDIATE command to perform DELETE or DROP operations on a table located on a remote SQL server (on-premises) using a JDBC connection from a notebook in the Databricks environment.&lt;/P&gt;&lt;P&gt;While I can successfully read from and write to the table on the remote SQL server using JDBC from the Databricks notebook, my attempts to execute DELETE and DROP commands have been unsuccessful.&lt;/P&gt;&lt;P&gt;Has anyone successfully used EXECUTE IMMEDIATE with a JDBC connection for such operations? Any help would be appreciated.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;This AI sample below is not working&amp;nbsp;&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;# Define your JDBC connection parameters&lt;BR /&gt;jdbc_url = "jdbc:sqlserver://your_server:1433;databaseName=your_database"&lt;BR /&gt;jdbc_properties = {&lt;BR /&gt;"user": "your_username",&lt;BR /&gt;"password": "your_password",&lt;BR /&gt;"driver": "com.microsoft.sqlserver.jdbc.SQLServerDriver"&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;# Name of the table you want to delete&lt;BR /&gt;table_name = "your_table_name"&lt;BR /&gt;&lt;BR /&gt;# SQL query to delete the table&lt;BR /&gt;delete_query = f"DROP TABLE IF EXISTS {table_name}"&lt;BR /&gt;&lt;BR /&gt;# Execute the delete query using Spark SQL&lt;BR /&gt;try:&lt;BR /&gt;spark.sql(f"""&lt;BR /&gt;EXECUTE IMMEDIATE '&lt;BR /&gt;{{&lt;BR /&gt;val conn = java.sql.DriverManager.getConnection("{jdbc_url}", "{jdbc_properties["user"]}", "{jdbc_properties["password"]}")&lt;BR /&gt;val stmt = conn.createStatement()&lt;BR /&gt;stmt.executeUpdate("{delete_query}")&lt;BR /&gt;stmt.close()&lt;BR /&gt;conn.close()&lt;BR /&gt;}}&lt;BR /&gt;'&lt;BR /&gt;""")&lt;BR /&gt;print(f"Table {table_name} has been successfully deleted.")&lt;BR /&gt;except Exception as e:&lt;BR /&gt;print(f"An error occurred while deleting the table: {str(e)}")&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 17 Oct 2024 20:06:38 GMT</pubDate>
    <dc:creator>HeronPePrestSer</dc:creator>
    <dc:date>2024-10-17T20:06:38Z</dc:date>
    <item>
      <title>EXECUTE IMMEDIATE works with JDBC connection ???</title>
      <link>https://community.databricks.com/t5/data-engineering/execute-immediate-works-with-jdbc-connection/m-p/94641#M38942</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hello, i need help&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to use the EXECUTE IMMEDIATE command to perform DELETE or DROP operations on a table located on a remote SQL server (on-premises) using a JDBC connection from a notebook in the Databricks environment.&lt;/P&gt;&lt;P&gt;While I can successfully read from and write to the table on the remote SQL server using JDBC from the Databricks notebook, my attempts to execute DELETE and DROP commands have been unsuccessful.&lt;/P&gt;&lt;P&gt;Has anyone successfully used EXECUTE IMMEDIATE with a JDBC connection for such operations? Any help would be appreciated.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;This AI sample below is not working&amp;nbsp;&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;# Define your JDBC connection parameters&lt;BR /&gt;jdbc_url = "jdbc:sqlserver://your_server:1433;databaseName=your_database"&lt;BR /&gt;jdbc_properties = {&lt;BR /&gt;"user": "your_username",&lt;BR /&gt;"password": "your_password",&lt;BR /&gt;"driver": "com.microsoft.sqlserver.jdbc.SQLServerDriver"&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;# Name of the table you want to delete&lt;BR /&gt;table_name = "your_table_name"&lt;BR /&gt;&lt;BR /&gt;# SQL query to delete the table&lt;BR /&gt;delete_query = f"DROP TABLE IF EXISTS {table_name}"&lt;BR /&gt;&lt;BR /&gt;# Execute the delete query using Spark SQL&lt;BR /&gt;try:&lt;BR /&gt;spark.sql(f"""&lt;BR /&gt;EXECUTE IMMEDIATE '&lt;BR /&gt;{{&lt;BR /&gt;val conn = java.sql.DriverManager.getConnection("{jdbc_url}", "{jdbc_properties["user"]}", "{jdbc_properties["password"]}")&lt;BR /&gt;val stmt = conn.createStatement()&lt;BR /&gt;stmt.executeUpdate("{delete_query}")&lt;BR /&gt;stmt.close()&lt;BR /&gt;conn.close()&lt;BR /&gt;}}&lt;BR /&gt;'&lt;BR /&gt;""")&lt;BR /&gt;print(f"Table {table_name} has been successfully deleted.")&lt;BR /&gt;except Exception as e:&lt;BR /&gt;print(f"An error occurred while deleting the table: {str(e)}")&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Oct 2024 20:06:38 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/execute-immediate-works-with-jdbc-connection/m-p/94641#M38942</guid>
      <dc:creator>HeronPePrestSer</dc:creator>
      <dc:date>2024-10-17T20:06:38Z</dc:date>
    </item>
    <item>
      <title>Re: EXECUTE IMMEDIATE works with JDBC connection ???</title>
      <link>https://community.databricks.com/t5/data-engineering/execute-immediate-works-with-jdbc-connection/m-p/96258#M39250</link>
      <description>&lt;P&gt;Hi - what error are you getting ? Do you have sufficient permission to drop / delete a table ?&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Sat, 26 Oct 2024 15:03:07 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/execute-immediate-works-with-jdbc-connection/m-p/96258#M39250</guid>
      <dc:creator>SathyaSDE</dc:creator>
      <dc:date>2024-10-26T15:03:07Z</dc:date>
    </item>
  </channel>
</rss>

