<?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 What is the equivalent of &amp;quot;if exists()&amp;quot; in databricks sql? in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/what-is-the-equivalent-of-quot-if-exists-quot-in-databricks-sql/m-p/82079#M36506</link>
    <description>&lt;P&gt;What is the equivalent of the below sql server syntax in databricks sql? there are cases where i need to execute a block of sql code on certain conditions. I know this can be achieved with spark.sql, but the problem with spark.sql()&amp;nbsp; is it does not provide the number of affected columns (insert/update/delete) after the execution of dml statement.&lt;/P&gt;&lt;P&gt;IF EXISTS(SELECT 1 FROM table WHERE col = value)&lt;/P&gt;&lt;P&gt;BEGIN&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;DELETE FROM table WHERE col = value&lt;/P&gt;&lt;P&gt;END&lt;/P&gt;&lt;P&gt;ELSE&lt;/P&gt;&lt;P&gt;BEGIN&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;lt;code to run in else&amp;gt;&lt;/P&gt;&lt;P&gt;END&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 06 Aug 2024 18:42:09 GMT</pubDate>
    <dc:creator>reachrishav</dc:creator>
    <dc:date>2024-08-06T18:42:09Z</dc:date>
    <item>
      <title>What is the equivalent of "if exists()" in databricks sql?</title>
      <link>https://community.databricks.com/t5/data-engineering/what-is-the-equivalent-of-quot-if-exists-quot-in-databricks-sql/m-p/82079#M36506</link>
      <description>&lt;P&gt;What is the equivalent of the below sql server syntax in databricks sql? there are cases where i need to execute a block of sql code on certain conditions. I know this can be achieved with spark.sql, but the problem with spark.sql()&amp;nbsp; is it does not provide the number of affected columns (insert/update/delete) after the execution of dml statement.&lt;/P&gt;&lt;P&gt;IF EXISTS(SELECT 1 FROM table WHERE col = value)&lt;/P&gt;&lt;P&gt;BEGIN&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;DELETE FROM table WHERE col = value&lt;/P&gt;&lt;P&gt;END&lt;/P&gt;&lt;P&gt;ELSE&lt;/P&gt;&lt;P&gt;BEGIN&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;lt;code to run in else&amp;gt;&lt;/P&gt;&lt;P&gt;END&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Aug 2024 18:42:09 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/what-is-the-equivalent-of-quot-if-exists-quot-in-databricks-sql/m-p/82079#M36506</guid>
      <dc:creator>reachrishav</dc:creator>
      <dc:date>2024-08-06T18:42:09Z</dc:date>
    </item>
    <item>
      <title>Re: What is the equivalent of "if exists()" in databricks sql?</title>
      <link>https://community.databricks.com/t5/data-engineering/what-is-the-equivalent-of-quot-if-exists-quot-in-databricks-sql/m-p/82341#M36615</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/115053"&gt;@reachrishav&lt;/a&gt;,&amp;nbsp;In Databricks SQL, you can replicate SQL Server's conditional logic using `CASE` statements and `MERGE` operations. Since Databricks SQL doesn't support `IF EXISTS` directly, you can create a temporary view to check your condition and then use `MERGE` to perform the desired operations. Afterward, you can count the affected rows with a `SELECT` statement. This approach allows for conditional execution and tracking of affected rows without relying on SQL Server-specific syntax.&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Does this help with your use case? If you have any more specific requirements or questions, feel free to ask!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Aug 2024 08:11:47 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/what-is-the-equivalent-of-quot-if-exists-quot-in-databricks-sql/m-p/82341#M36615</guid>
      <dc:creator>Retired_mod</dc:creator>
      <dc:date>2024-08-08T08:11:47Z</dc:date>
    </item>
  </channel>
</rss>

