<?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 Re: SQLServer Incorrect syntax near the keyword 'WITH' in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/sqlserver-incorrect-syntax-near-the-keyword-with/m-p/78413#M35521</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/27091"&gt;@Jreco&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;You need to use prepare query option and then query like below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;url = "jdbc:sqlserver://server_name:1433;database=db_name"
df = spark.read \
    .format("jdbc") \
    .option("url", url) \
    .option("prepareQuery", "with cte as ( SELECT TOP 10 * FROM dbo.TestTb wt)") \
    .option("query", "SELECT * FROM cte") \
    .option("user", "user_name") \
    .option("password", "password")
    .option("driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver").load()   
    &lt;/LI-CODE&gt;&lt;P&gt;I've tested it and it works &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Slash_0-1720728701965.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/9512iEC9F5E41DB2EE6B7/image-size/medium/is-moderation-mode/true?v=v2&amp;amp;px=400" role="button" title="Slash_0-1720728701965.png" alt="Slash_0-1720728701965.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 11 Jul 2024 20:11:48 GMT</pubDate>
    <dc:creator>szymon_dybczak</dc:creator>
    <dc:date>2024-07-11T20:11:48Z</dc:date>
    <item>
      <title>SQLServer Incorrect syntax near the keyword 'WITH'</title>
      <link>https://community.databricks.com/t5/data-engineering/sqlserver-incorrect-syntax-near-the-keyword-with/m-p/78403#M35516</link>
      <description>&lt;P&gt;Hi Mates!&lt;BR /&gt;&lt;BR /&gt;I'm trying to get some data from an SQLServer using a query; the query has a WITH statement but I'm getting the following error:&lt;BR /&gt;raise convert_exception(&lt;BR /&gt;pyspark.errors.exceptions.connect.SparkConnectGrpcException: (com.microsoft.sqlserver.jdbc.SQLServerException) Incorrect syntax near the keyword 'WITH'.&lt;BR /&gt;This is my code for the example:&lt;/P&gt;&lt;DIV&gt;&lt;PRE&gt;query = &lt;SPAN&gt;"""with cte as &lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt; SELECT TOP 10 * FROM dbo.TestTb wt&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;SPAN&gt; select * from cte"""&lt;BR /&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;DIV&gt;&lt;PRE&gt;df = spark.read \&lt;BR /&gt;    .&lt;SPAN&gt;format&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;"jdbc"&lt;/SPAN&gt;&lt;SPAN&gt;) &lt;/SPAN&gt;\&lt;BR /&gt;    .&lt;SPAN&gt;option&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;"url"&lt;/SPAN&gt;, sqlserver_option&lt;SPAN&gt;[&lt;/SPAN&gt;&lt;SPAN&gt;"url"&lt;/SPAN&gt;&lt;SPAN&gt;]) &lt;/SPAN&gt;\&lt;BR /&gt;    .&lt;SPAN&gt;option&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;"dbtable"&lt;/SPAN&gt;, query&lt;SPAN&gt;) &lt;/SPAN&gt;\&lt;BR /&gt;    .&lt;SPAN&gt;option&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;"user"&lt;/SPAN&gt;, sqlserver_option&lt;SPAN&gt;[&lt;/SPAN&gt;&lt;SPAN&gt;"user"&lt;/SPAN&gt;&lt;SPAN&gt;]) &lt;/SPAN&gt;\&lt;BR /&gt;    .&lt;SPAN&gt;option&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;"password"&lt;/SPAN&gt;, sqlserver_option&lt;SPAN&gt;[&lt;/SPAN&gt;&lt;SPAN&gt;"password"&lt;/SPAN&gt;&lt;SPAN&gt;]) &lt;/SPAN&gt;\&lt;BR /&gt;    .&lt;SPAN&gt;option&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;"driver"&lt;/SPAN&gt;, &lt;SPAN&gt;"com.microsoft.sqlserver.jdbc.SQLServerDriver"&lt;/SPAN&gt;&lt;SPAN&gt;) &lt;/SPAN&gt;\&lt;BR /&gt;    .&lt;SPAN&gt;load&lt;/SPAN&gt;&lt;SPAN&gt;()&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;I also tried to use: .&lt;SPAN&gt;option&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;"query"&lt;/SPAN&gt;, query&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;Both options return the same error.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Looking for similar cases, I found that in 2022 this issue was solved:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;A href="https://github.com/apache/spark/pull/28953" target="_blank" rel="noopener"&gt;https://github.com/apache/spark/pull/28953&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;A href="https://github.com/microsoft/sql-spark-connector/issues/147" target="_blank" rel="noopener"&gt;https://github.com/microsoft/sql-spark-connector/issues/147&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;A href="https://github.com/apache/spark/pull/36440" target="_blank" rel="noopener"&gt;https://github.com/apache/spark/pull/36440&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Is it possible I'm doing something wrong?&lt;BR /&gt;&lt;BR /&gt;Can someone give me a guide in this matter?&lt;BR /&gt;&lt;BR /&gt;Thanks!!&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Thu, 11 Jul 2024 17:44:43 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/sqlserver-incorrect-syntax-near-the-keyword-with/m-p/78403#M35516</guid>
      <dc:creator>Jreco</dc:creator>
      <dc:date>2024-07-11T17:44:43Z</dc:date>
    </item>
    <item>
      <title>Re: SQLServer Incorrect syntax near the keyword 'WITH'</title>
      <link>https://community.databricks.com/t5/data-engineering/sqlserver-incorrect-syntax-near-the-keyword-with/m-p/78413#M35521</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/27091"&gt;@Jreco&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;You need to use prepare query option and then query like below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;url = "jdbc:sqlserver://server_name:1433;database=db_name"
df = spark.read \
    .format("jdbc") \
    .option("url", url) \
    .option("prepareQuery", "with cte as ( SELECT TOP 10 * FROM dbo.TestTb wt)") \
    .option("query", "SELECT * FROM cte") \
    .option("user", "user_name") \
    .option("password", "password")
    .option("driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver").load()   
    &lt;/LI-CODE&gt;&lt;P&gt;I've tested it and it works &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Slash_0-1720728701965.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/9512iEC9F5E41DB2EE6B7/image-size/medium/is-moderation-mode/true?v=v2&amp;amp;px=400" role="button" title="Slash_0-1720728701965.png" alt="Slash_0-1720728701965.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jul 2024 20:11:48 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/sqlserver-incorrect-syntax-near-the-keyword-with/m-p/78413#M35521</guid>
      <dc:creator>szymon_dybczak</dc:creator>
      <dc:date>2024-07-11T20:11:48Z</dc:date>
    </item>
    <item>
      <title>Re: SQLServer Incorrect syntax near the keyword 'WITH'</title>
      <link>https://community.databricks.com/t5/data-engineering/sqlserver-incorrect-syntax-near-the-keyword-with/m-p/78424#M35524</link>
      <description>&lt;P&gt;Thanks for your response&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/110502"&gt;@szymon_dybczak&lt;/a&gt;&amp;nbsp;was helpful!&lt;BR /&gt;&lt;BR /&gt;Do you know where I can get some additional information about those options?&lt;BR /&gt;&lt;BR /&gt;Thank you so much!&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jul 2024 23:11:11 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/sqlserver-incorrect-syntax-near-the-keyword-with/m-p/78424#M35524</guid>
      <dc:creator>Jreco</dc:creator>
      <dc:date>2024-07-11T23:11:11Z</dc:date>
    </item>
  </channel>
</rss>

