<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Eddie Awad’s Blog - Latest Comments in CASE gotcha in Oracle 8i</title><link>http://awads.disqus.com/</link><description>News, views, tips and tricks on Oracle and other fun stuff</description><atom:link href="https://awads.disqus.com/case_gotcha_in_oracle_8i/latest.rss" rel="self"></atom:link><language>en</language><lastBuildDate>Mon, 10 Mar 2008 13:49:13 -0000</lastBuildDate><item><title>Re: CASE gotcha in Oracle 8i</title><link>http://awads.net/wp/2005/10/20/case-gotcha-in-oracle-8i/#comment-3657972</link><description>&lt;p&gt;@santhosh: You can try something like the following, or use  &lt;a href="http://www.oracle.com/pls/db102/print_hit_summary?search_string=dbms_sql" rel="nofollow noopener" target="_blank" title="http://www.oracle.com/pls/db102/print_hit_summary?search_string=dbms_sql"&gt;dbms_sql&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;pre&gt;DECLARE&lt;br&gt;  l_my_cursor   sys_refcursor;&lt;br&gt;  l_stmt_str    VARCHAR2(32767);&lt;br&gt;  l_var         NUMBER;&lt;br&gt;BEGIN&lt;br&gt;  l_stmt_str := &lt;br&gt;  'SELECT CASE WHEN 1=1 THEN 1 ELSE 2 END FROM dual&lt;br&gt;   UNION&lt;br&gt;   SELECT CASE WHEN 1=2 THEN 1 ELSE 2 END FROM dual';&lt;br&gt;  OPEN l_my_cursor FOR l_stmt_str; &lt;br&gt;    LOOP&lt;br&gt;      FETCH l_my_cursor &lt;br&gt;        INTO l_var; &lt;br&gt;       EXIT WHEN l_my_cursor%NOTFOUND;&lt;br&gt;      dbms_output.put_line('l_var='||to_char(l_var));&lt;br&gt;    END LOOP; &lt;br&gt;  CLOSE l_my_cursor;&lt;br&gt;END;  &lt;br&gt;&lt;/pre&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Eddie Awad</dc:creator><pubDate>Mon, 10 Mar 2008 13:49:13 -0000</pubDate></item><item><title>Re: CASE gotcha in Oracle 8i</title><link>http://awads.net/wp/2005/10/20/case-gotcha-in-oracle-8i/#comment-3657971</link><description>&lt;p&gt;apps@dev1&amp;gt; declare&lt;br&gt;  2      l_var number;&lt;br&gt;  3      sql_string varchar2(100);&lt;br&gt;  4  begin&lt;br&gt;  5      sql_string := 'select case when 1=1 then 1 else 2 end ' ||&lt;br&gt;  6          'from dual';&lt;br&gt;  7      execute immediate sql_string into l_var;&lt;br&gt;  8      dbms_output.put_line('l_var='||to_char(l_var));&lt;br&gt;  9  end;&lt;br&gt; 10  /&lt;br&gt;l_var=1&lt;/p&gt;&lt;p&gt;The above is applicable, when the sql returns only one value (1 row, 1 column)&lt;/p&gt;&lt;p&gt;How do I implement the same using curser,&lt;br&gt;ie, if the sql statement returns a tabular data (M rows, N columns:: where M &amp;amp; N &amp;gt;2)?&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">santhosh</dc:creator><pubDate>Mon, 10 Mar 2008 08:14:52 -0000</pubDate></item><item><title>Re: CASE gotcha in Oracle 8i</title><link>http://awads.net/wp/2005/10/20/case-gotcha-in-oracle-8i/#comment-3657970</link><description>&lt;p&gt;Excuse me, i don't understand...&lt;br&gt;How i can you DECODE functions?&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Oracle Functions</dc:creator><pubDate>Sat, 31 Mar 2007 03:11:24 -0000</pubDate></item><item><title>Re: CASE gotcha in Oracle 8i</title><link>http://awads.net/wp/2005/10/20/case-gotcha-in-oracle-8i/#comment-3657969</link><description>&lt;p&gt;Steve, &lt;br&gt;Your SQL syntax does not look correct. However, I tried the following in 10gXE:&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;pre&gt;SELECT (CASE&lt;br&gt;           WHEN 1 = 1&lt;br&gt;              THEN CURSOR (SELECT 1&lt;br&gt;                             FROM DUAL)&lt;br&gt;           WHEN 1 = 2&lt;br&gt;              THEN CURSOR (SELECT 1&lt;br&gt;                             FROM DUAL)&lt;br&gt;           ELSE NULL&lt;br&gt;        END&lt;br&gt;       ) x&lt;br&gt;  FROM DUAL&lt;br&gt;/&lt;br&gt;&lt;/pre&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;It returned an error:&lt;br&gt;ORA-22902: CURSOR expression not allowed &lt;br&gt;Cause: CURSOR on a subquery is allowed only in the top-level SELECT list of a query.&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;That means you cannot use cursor expressions in a CASE or even DECODE.&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;Another solution may be the use of Dynamic SQL.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Eddie Awad</dc:creator><pubDate>Sat, 03 Jun 2006 00:34:43 -0000</pubDate></item><item><title>Re: CASE gotcha in Oracle 8i</title><link>http://awads.net/wp/2005/10/20/case-gotcha-in-oracle-8i/#comment-3657968</link><description>&lt;p&gt;so how would you code something that returns a cursor nested in a table based on a case statment in 9i?&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;select *,&lt;br&gt;(case &lt;br&gt;when tb1.item=100 then&lt;br&gt;cursor(select * from tb2 where id=5 from mytable2 tb2)&lt;br&gt;when tb2.item=200 then&lt;br&gt;cursor(select * from tb3 where id=6 from mytable3 tb3)&lt;br&gt;else null&lt;br&gt;end ) curval&lt;br&gt;from mytable1&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;or would you have to used "decode"&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">STEVE</dc:creator><pubDate>Tue, 30 May 2006 19:18:22 -0000</pubDate></item><item><title>Re: CASE gotcha in Oracle 8i</title><link>http://awads.net/wp/2005/10/20/case-gotcha-in-oracle-8i/#comment-3657964</link><description>&lt;p&gt;SaSantos, what do you mean by "heavy"? Have you measured the performance of your query and found it to be slow? Have you run your query through an explain plan? a tkprof?&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;I suggest you post your question on &lt;a href="http://forums.oracle.com" rel="nofollow noopener" target="_blank" title="forums.oracle.com"&gt;forums.oracle.com&lt;/a&gt;. I also suggest you read:&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="http://oraclesponge.blogspot.com/2005/04/writing-good-sql.html" rel="nofollow noopener" target="_blank" title="http://oraclesponge.blogspot.com/2005/04/writing-good-sql.html"&gt;http://oraclesponge.blogspot.com/2005/04/writing-good-sql.html&lt;/a&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Eddie Awad</dc:creator><pubDate>Mon, 23 Jan 2006 18:44:36 -0000</pubDate></item><item><title>Re: CASE gotcha in Oracle 8i</title><link>http://awads.net/wp/2005/10/20/case-gotcha-in-oracle-8i/#comment-3657966</link><description>&lt;p&gt;Eddie,&lt;br&gt;the ideal would be to only run the first OR if there weren't any rows returned for the "first conditions":&lt;br&gt;   (to_char(Adate, 'YYYYMMDD') = to_char(I_date, 'YYYYMMDD')&lt;br&gt;   and "more_conditions")&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;And run the second OR if the:&lt;br&gt;(to_char(Adate, 'YYYYMM') = to_char(I_date, 'YYYYMM')&lt;br&gt;and "more_conditions")&lt;br&gt;DidnÂ´t returned rows.&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;But with the OR's or with the UNION's the query is to "heavy".&lt;br&gt;Thats why the ideal would be run the query (based on three small queries) from the start to end, and "stop" at the first that returned rows.&lt;br&gt;I don't know if thats possible. Besides the optional solution I refered before.&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;TIA,&lt;br&gt;SaSantos&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">SaSantos</dc:creator><pubDate>Mon, 23 Jan 2006 16:42:00 -0000</pubDate></item><item><title>Re: CASE gotcha in Oracle 8i</title><link>http://awads.net/wp/2005/10/20/case-gotcha-in-oracle-8i/#comment-3657965</link><description>&lt;p&gt;SASantos, it looks like you are selecting the same fields from the same table in all three queries, So, why don't you use one query like this:&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;select "fields"&lt;br&gt;from table&lt;br&gt;where &lt;br&gt;(to_char(Adate, 'YYYYMMDD') = to_char(I_date, 'YYYYMMDD')&lt;br&gt;and "more_conditions") &lt;br&gt;OR&lt;br&gt;(to_char(Adate, 'YYYYMM') = to_char(I_date, 'YYYYMM')&lt;br&gt;and "more_conditions")&lt;br&gt;OR&lt;br&gt;(to_char(Adate, 'YYYY') = to_char(I_date, 'YYYY')&lt;br&gt;and "more_conditions")&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;Moreover, if Adate and I_date are of a date datatype, why converting to to_char? you can just use TRUNC.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Eddie Awad</dc:creator><pubDate>Sun, 22 Jan 2006 21:33:18 -0000</pubDate></item><item><title>Re: CASE gotcha in Oracle 8i</title><link>http://awads.net/wp/2005/10/20/case-gotcha-in-oracle-8i/#comment-3657967</link><description>&lt;p&gt;Hi!&lt;br&gt;I'm using Oracle's version 8.1.7. &lt;br&gt;There's a cursor query in a PL/SQL package like the following one:&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;select "fields",&lt;br&gt;            1 qry_condition&lt;br&gt;  from table&lt;br&gt;where to_char(Adate, 'YYYYMMDD') = to_char(I_date, 'YYYYMMDD')&lt;br&gt;    and "more_conditions"&lt;br&gt;union all&lt;br&gt;select "fields",&lt;br&gt;            2 qry_condition&lt;br&gt;  from table&lt;br&gt;where to_char(Adate, 'YYYYMM') = to_char(I_date, 'YYYYMM')&lt;br&gt;    and "more_conditions"&lt;br&gt;union all&lt;br&gt;select "fields",&lt;br&gt;            3 qry_condition&lt;br&gt;  from table&lt;br&gt;where to_char(Adate, 'YYYY') = to_char(I_date, 'YYYY')&lt;br&gt;    and "more_conditions"&lt;br&gt;order by "2" -- qry_condition;&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;The idea is to execute (only) the first query if results are returned, execute the next query if the first doesn't returned rows, and execute the third query if none of the previous two return data. &lt;br&gt;But as we can conclude, everytime this cursor is executed it runs&lt;br&gt;all of the three queries that belong to it, which is impacting negatively the cursor perfomance.&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;One possible solution would be to divide it in three querys and use,&lt;br&gt;something like this:&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;open cursor1(L_date);&lt;br&gt;fetch cursor1 into ...&lt;br&gt;if cursor1.notfound &lt;br&gt;then&lt;br&gt;   open cursor2(L_date);&lt;br&gt;   fetch cursor2 into ...&lt;br&gt;   if cursor2.notfound &lt;br&gt;   then&lt;br&gt;      open cursor3(L_date);&lt;br&gt;      fetch cursor3 into ...&lt;br&gt;      if cursor3.notfound then&lt;br&gt;      (...)&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;end if;&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;(...)&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;But personally I dont like this solution. I was thinking in use the CASE keyword to help me solving it, continuing to have only a single cursor. Something like this (I don't know if this is possible):&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;select "fields",&lt;br&gt;            1 qry_condition&lt;br&gt;  from table&lt;br&gt;where (&lt;br&gt;CASE when to_char(Adate, 'YYYYMMDD') = to_char(I_date, 'YYYYMMDD')&lt;br&gt;            and "more_conditions"&lt;br&gt;          then 1&lt;br&gt;          ----&lt;br&gt;          when to_char(Adate, 'YYYYMM') = to_char(I_date, 'YYYYMM')&lt;br&gt;             and "more_conditions"&lt;br&gt;          then 2&lt;br&gt;          ----&lt;br&gt;          when to_char(Adate, 'YYYY') = to_char(I_date, 'YYYY')&lt;br&gt;             and "more_conditions"&lt;br&gt;          then 3&lt;br&gt;         end IN (1, 2, 3)&lt;br&gt;;&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;I know that this Oracle version of the PL/SQL doesn't support the CASE, so I was thinking in using dynamic Sql to do it.&lt;br&gt;What's your opinion,  does the CASE solves my request or do you have a better solution?&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;Thanks in advance&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;SASantos&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">"Hierarchy" cursor query</dc:creator><pubDate>Sat, 21 Jan 2006 09:39:11 -0000</pubDate></item><item><title>Re: CASE gotcha in Oracle 8i</title><link>http://awads.net/wp/2005/10/20/case-gotcha-in-oracle-8i/#comment-3657963</link><description>&lt;p&gt;Ahhh.. wikipedia... didn't think of that.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Tom_Fox</dc:creator><pubDate>Fri, 21 Oct 2005 14:58:53 -0000</pubDate></item><item><title>Re: CASE gotcha in Oracle 8i</title><link>http://awads.net/wp/2005/10/20/case-gotcha-in-oracle-8i/#comment-3657962</link><description>&lt;p&gt;Here is the Oracle DB version history:&lt;/p&gt;&lt;p&gt;&lt;br&gt;&amp;lt;ul&amp;gt;&lt;br&gt;&amp;lt;li&amp;gt;Oracle7: 7.0.16 - 7.3.4&amp;lt;/li&amp;gt;&lt;br&gt;&amp;lt;li&amp;gt;Oracle8 Database: 8.0.3 - 8.0.6&amp;lt;/li&amp;gt;&lt;br&gt;&amp;lt;li&amp;gt;Oracle8i Database Release 1: 8.1.5.0 - 8.1.5.1&amp;lt;/li&amp;gt;&lt;br&gt;&amp;lt;li&amp;gt;Oracle8i Database Release 2: 8.1.6.0 - 8.1.6.3&amp;lt;/li&amp;gt;&lt;br&gt;&amp;lt;li&amp;gt;Oracle8i Database Release 3: 8.1.7.0 - 8.1.7.4&amp;lt;/li&amp;gt;&lt;br&gt;&amp;lt;li&amp;gt;Oracle9i Database Release 1:&lt;br&gt;&amp;lt;/li&amp;gt;&amp;lt;/ul&amp;gt;&amp;lt;ul&amp;gt;&amp;lt;li&amp;gt;9.0.1.0, May 2002&amp;lt;/li&amp;gt;&lt;br&gt;&amp;lt;li&amp;gt;9.0.1.5&amp;lt;/li&amp;gt;&amp;lt;/ul&amp;gt;&lt;br&gt;&amp;lt;li&amp;gt;Oracle9i Database Release 2: 9.2.0.1 - 9.2.0.7 (Latest current patchset as of August 2005)&amp;lt;/li&amp;gt;&lt;br&gt;&amp;lt;li&amp;gt;Oracle Database 10g Release 1: 10.1.0.2 - 10.1.0.4 (Latest current patchset as of March 2005)&amp;lt;/li&amp;gt;&lt;br&gt;&amp;lt;li&amp;gt;Oracle Database 10g Release 2: 10.2.0.1&amp;lt;/li&amp;gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Oracle_database#Version_numbering_conventions" rel="nofollow noopener" target="_blank" title="http://en.wikipedia.org/wiki/Oracle_database#Version_numbering_conventions"&gt;source&lt;/a&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Eddie Awad</dc:creator><pubDate>Fri, 21 Oct 2005 12:46:58 -0000</pubDate></item><item><title>Re: CASE gotcha in Oracle 8i</title><link>http://awads.net/wp/2005/10/20/case-gotcha-in-oracle-8i/#comment-3657961</link><description>&lt;p&gt;What version did 8i start with? 8.1.5?  I'm new to the Oracle world, and the version numbers confuse me.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Tom_Fox</dc:creator><pubDate>Fri, 21 Oct 2005 12:14:52 -0000</pubDate></item><item><title>Re: CASE gotcha in Oracle 8i</title><link>http://awads.net/wp/2005/10/20/case-gotcha-in-oracle-8i/#comment-3657960</link><description>&lt;p&gt;"but looks like Tomâ€™s â€œtransactionâ€ was â€œcommittedâ€ before Dougâ€™s"&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;Surprise, surprise ... how could anyone keep up!&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;;-)&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Doug Burns</dc:creator><pubDate>Thu, 20 Oct 2005 18:48:17 -0000</pubDate></item><item><title>Re: CASE gotcha in Oracle 8i</title><link>http://awads.net/wp/2005/10/20/case-gotcha-in-oracle-8i/#comment-3657959</link><description>&lt;p&gt;Wow! what a coincidence, both Tom and Doug submitted their comments at the same time, but looks like Tom's "transaction" was "committed" before Doug's :)&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;Thanks Tom for the trick of using dynamic SQL to work around using CASE in PL/SQL prior to 9.0.1. Here is a quick example:&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;pre&gt;Connected to:&lt;br&gt;Oracle8i Enterprise Edition Release 8.1.7.4.0 - Production&lt;br&gt;With the Partitioning option&lt;br&gt;JServer Release 8.1.7.4.0 - Production&lt;br&gt;&lt;br&gt;apps@dev1&amp;gt; declare&lt;br&gt;  2      l_var number;&lt;br&gt;  3      sql_string varchar2(100);&lt;br&gt;  4  begin&lt;br&gt;  5      sql_string := 'select case when 1=1 then 1 else 2 end ' ||&lt;br&gt;  6          'from dual';&lt;br&gt;  7      execute immediate sql_string into l_var;&lt;br&gt;  8      dbms_output.put_line('l_var='||to_char(l_var));&lt;br&gt;  9  end;&lt;br&gt; 10  /&lt;br&gt;l_var=1&lt;br&gt;&lt;br&gt;PL/SQL procedure successfully completed.&lt;br&gt;&lt;/pre&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Eddie Awad</dc:creator><pubDate>Thu, 20 Oct 2005 14:46:04 -0000</pubDate></item><item><title>Re: CASE gotcha in Oracle 8i</title><link>http://awads.net/wp/2005/10/20/case-gotcha-in-oracle-8i/#comment-3657958</link><description>&lt;p&gt;Good point Eddie. I was well aware of that (it was very frustrating until Oracle sorted out) but thought that it was clear enough that the focus in the paper was on SQL.&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;I'll think about mentioning it, though.&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;Cheers,&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;Doug&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Doug Burns</dc:creator><pubDate>Thu, 20 Oct 2005 14:27:47 -0000</pubDate></item><item><title>Re: CASE gotcha in Oracle 8i</title><link>http://awads.net/wp/2005/10/20/case-gotcha-in-oracle-8i/#comment-3657957</link><description>&lt;p&gt;(native) dynamic sql - you can use that to work around this.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Thomas Kyte</dc:creator><pubDate>Thu, 20 Oct 2005 14:27:16 -0000</pubDate></item></channel></rss>