<?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>Question Re: IF statement syntax in SQL script view in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/if-statement-syntax-in-sql-script-view/qaa-p/10531636#M3827248</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Chandra and Sreehari but for the logic that I need to use, CASE wont work.&lt;/P&gt;&lt;P&gt;I have used CASE too for a different requirement and that works just fine.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If I get to know the exact IF syntax (not sure if it can be used in SELECT though) then I can use that to exactly use the logic I need.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 19 Aug 2014 13:52:27 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2014-08-19T13:52:27Z</dc:date>
    <item>
      <title>IF statement syntax in SQL script view</title>
      <link>https://community.sap.com/t5/technology-q-a/if-statement-syntax-in-sql-script-view/qaq-p/10531631</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I need to include a "IF" condition in the "SELECT" section of my SQL script view. &lt;/P&gt;&lt;P&gt;I tried the following syntax's but I get the error 'Incorrect SQL syntax near 'IF'&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1.&amp;nbsp; IF(Revenue &amp;lt;&amp;gt; '0' AND Quantity &amp;lt;&amp;gt; '0', Revenue/Quantity, '0') AS Gross Price&lt;/P&gt;&lt;P&gt;2.&amp;nbsp; IF(Revenue != '0' AND Quantity != '0', Revenue/Quantity, '0') AS Gross Price&lt;/P&gt;&lt;P&gt;3.&amp;nbsp; IF(Revenue &amp;lt;&amp;gt; '0' AND Quantity &amp;lt;&amp;gt; '0' THEN Revenue/Quantity ELSE&amp;nbsp; '0' END) AS Gross Price&lt;/P&gt;&lt;P&gt;4.&amp;nbsp; IF(Revenue != '0' AND Quantity != '0' THEN Revenue/Quantity ELSE&amp;nbsp; '0' END) AS Gross Price&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My final SQL would read like follows:&lt;/P&gt;&lt;P&gt;SELECT field1, field2, IF(......) AS field3&lt;/P&gt;&lt;P&gt;FROM table1&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Can anybody please help with the correct IF statement syntax to be used in the SQL script based view?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Aug 2014 11:58:21 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/if-statement-syntax-in-sql-script-view/qaq-p/10531631</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2014-08-19T11:58:21Z</dc:date>
    </item>
    <item>
      <title>Re: IF statement syntax in SQL script view</title>
      <link>https://community.sap.com/t5/technology-q-a/if-statement-syntax-in-sql-script-view/qaa-p/10531632#M3827244</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Lakshmi,&lt;/P&gt;&lt;P&gt;below is the syntax for IF statement.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;IF &amp;lt;bool_expr1&amp;gt; THEN&lt;/P&gt;&lt;P&gt;&amp;lt;then_stmts1&amp;gt;&lt;/P&gt;&lt;P&gt;[&lt;/P&gt;&lt;P&gt;{&lt;/P&gt;&lt;P&gt;ELSEIF &amp;lt;bool_expr2&amp;gt;&lt;/P&gt;&lt;P&gt;THEN &amp;lt;then_stmts2&amp;gt;&lt;/P&gt;&lt;P&gt;}...]&lt;/P&gt;&lt;P&gt;[ELSE &amp;lt;else_stmts3&amp;gt;]&lt;/P&gt;&lt;P&gt;END IF&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;eg :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;BEGIN&lt;/P&gt;&lt;P&gt;DECLARE found INT := 1;&lt;/P&gt;&lt;P&gt;SELECT count(*) INTO found FROM books WHERE isbn = :v_isbn;&lt;/P&gt;&lt;P&gt;IF :found = 0 THEN&lt;/P&gt;&lt;P&gt;INSERT INTO books VALUES (:v_isbn, 'In-Memory Data Management', 1, 1, '2011', 42.75, 'EUR');&lt;/P&gt;&lt;P&gt;ELSE&lt;/P&gt;&lt;P&gt;UPDATE books SET price = 42.75 WHERE isbn =:v_isbn;&lt;/P&gt;&lt;P&gt;END IF;&lt;/P&gt;&lt;P&gt;END;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Sreehari&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Aug 2014 12:11:12 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/if-statement-syntax-in-sql-script-view/qaa-p/10531632#M3827244</guid>
      <dc:creator>sreehari_vpillai</dc:creator>
      <dc:date>2014-08-19T12:11:12Z</dc:date>
    </item>
    <item>
      <title>Re: IF statement syntax in SQL script view</title>
      <link>https://community.sap.com/t5/technology-q-a/if-statement-syntax-in-sql-script-view/qaa-p/10531633#M3827245</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Sreehari,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But I will have to include "IF" in the 'SELECT' clause itself just before 'FROM' as follows:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;SELECT Revenue, Quantity, IF(...condition...) AS field3&lt;/P&gt;&lt;P&gt;FROM table1&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Please let me know if any inputs.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Aug 2014 12:19:06 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/if-statement-syntax-in-sql-script-view/qaa-p/10531633#M3827245</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2014-08-19T12:19:06Z</dc:date>
    </item>
    <item>
      <title>Re: IF statement syntax in SQL script view</title>
      <link>https://community.sap.com/t5/technology-q-a/if-statement-syntax-in-sql-script-view/qaa-p/10531634#M3827246</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Lakshmi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;How about CASE instead of IF? Even I have used CASE for validations in SELECT statement, IF didn't work.&lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;/P&gt;&lt;P&gt;SELECT REVENUE, QUANTITY,CASE WHEN REVENUE &amp;lt;&amp;gt; '0' AND QUANTITY &amp;lt;&amp;gt; '0' THEN REVENUE/QUANTITY ELSE '0' END AS GROSS_PRICE&lt;/P&gt;&lt;P&gt;FROM TABLE1;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This query should work.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Chandu.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Aug 2014 12:31:48 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/if-statement-syntax-in-sql-script-view/qaa-p/10531634#M3827246</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2014-08-19T12:31:48Z</dc:date>
    </item>
    <item>
      <title>Re: IF statement syntax in SQL script view</title>
      <link>https://community.sap.com/t5/technology-q-a/if-statement-syntax-in-sql-script-view/qaa-p/10531635#M3827247</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I don't thing IF is supported here. Would case statement help you ?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;select MATNR ,&lt;/P&gt;&lt;P&gt;CASE&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp; WHEN "ERSDA" &amp;gt; '20130405' THEN 'Y'&lt;/P&gt;&lt;P&gt;&amp;nbsp; ELSE 'N'&lt;/P&gt;&lt;P&gt;&amp;nbsp; END as "TEST"&lt;/P&gt;&lt;P&gt;from "SAPXX"."MARA"; You can change your IF condition this way. &lt;SPAN __jive_emoticon_name="happy" __jive_macro_name="emoticon" class="jive_macro jive_emote" src="https://community.sap.com/994/images/emoticons/happy.gif"&gt;&lt;/SPAN&gt; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Sree&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Message was edited by: Sreehari V Pillai&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Aug 2014 12:43:40 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/if-statement-syntax-in-sql-script-view/qaa-p/10531635#M3827247</guid>
      <dc:creator>sreehari_vpillai</dc:creator>
      <dc:date>2014-08-19T12:43:40Z</dc:date>
    </item>
    <item>
      <title>Re: IF statement syntax in SQL script view</title>
      <link>https://community.sap.com/t5/technology-q-a/if-statement-syntax-in-sql-script-view/qaa-p/10531636#M3827248</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Chandra and Sreehari but for the logic that I need to use, CASE wont work.&lt;/P&gt;&lt;P&gt;I have used CASE too for a different requirement and that works just fine.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If I get to know the exact IF syntax (not sure if it can be used in SELECT though) then I can use that to exactly use the logic I need.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Aug 2014 13:52:27 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/if-statement-syntax-in-sql-script-view/qaa-p/10531636#M3827248</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2014-08-19T13:52:27Z</dc:date>
    </item>
    <item>
      <title>Re: IF statement syntax in SQL script view</title>
      <link>https://community.sap.com/t5/technology-q-a/if-statement-syntax-in-sql-script-view/qaa-p/10531637#M3827249</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Lakshmi, can you tell us your requirement so that we will see if we can help you.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 20 Aug 2014 05:08:18 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/if-statement-syntax-in-sql-script-view/qaa-p/10531637#M3827249</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2014-08-20T05:08:18Z</dc:date>
    </item>
    <item>
      <title>Re: IF statement syntax in SQL script view</title>
      <link>https://community.sap.com/t5/technology-q-a/if-statement-syntax-in-sql-script-view/qaa-p/10531638#M3827250</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Lakshmi&lt;/P&gt;&lt;P&gt;im pretty sure you can't use IF inside a SELECT statement as you just said. &lt;/P&gt;&lt;P&gt;The IF statement is to control the flow of logic I.e. To decide which statements to execute in the script. &lt;/P&gt;&lt;P&gt;I Think if you check the syntax for SELECT in the SQL Reference guide you'll find this to be true. &lt;/P&gt;&lt;P&gt;you should be able to get rid of the IF and turn your expression into a 'conditional expression' that uses a logical test and Boolean results in the arithmetic to return the result you need. &lt;/P&gt;&lt;P&gt;Something like &lt;/P&gt;&lt;P&gt;(expression=value*value + expression&amp;lt;&amp;gt;value*X) AS colname&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;there is no doubt a more elegant way of doing that most likely using CASE but I'm off to meeting and don't have time to check it out at the moment &lt;/P&gt;&lt;P&gt;hope this helps &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 20 Aug 2014 07:41:23 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/if-statement-syntax-in-sql-script-view/qaa-p/10531638#M3827250</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2014-08-20T07:41:23Z</dc:date>
    </item>
    <item>
      <title>Re: IF statement syntax in SQL script view</title>
      <link>https://community.sap.com/t5/technology-q-a/if-statement-syntax-in-sql-script-view/qaa-p/10531639#M3827251</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Lakshmi,&lt;/P&gt;&lt;P&gt;You can not use IF along with SELECT statement. Note that, you can achieve most of boolean logics with CASE statement syntax as noted by &lt;A __default_attr="85499" __jive_macro_name="user" class="jive_macro_user jive_macro" data-orig-content="Chandra Sekhar" href="https://community.sap.com/"&gt;&lt;/A&gt; . In select, you are applying it over a column and your logic will be executed as many as times the count of result set rows. Hence , righting an imperative logic is not well appreciated. Still, if you want to do the same, create a calculation view and use intermediate calculated columns to achieve what you are expecting . &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Sreehari&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 20 Aug 2014 08:33:00 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/if-statement-syntax-in-sql-script-view/qaa-p/10531639#M3827251</guid>
      <dc:creator>sreehari_vpillai</dc:creator>
      <dc:date>2014-08-20T08:33:00Z</dc:date>
    </item>
    <item>
      <title>Re: IF statement syntax in SQL script view</title>
      <link>https://community.sap.com/t5/technology-q-a/if-statement-syntax-in-sql-script-view/qaa-p/10531640#M3827252</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you Sreehari, Chandra and Henry for the responses.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The SQL view is built on top of a graphical view, so I will write a calculated column in that and try to use it in my SQL view.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 20 Aug 2014 08:41:05 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/if-statement-syntax-in-sql-script-view/qaa-p/10531640#M3827252</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2014-08-20T08:41:05Z</dc:date>
    </item>
  </channel>
</rss>

