DECODE is not working in WHERE clause when subquery returns more rows

Hi Gurus,
I want to write a query on CCENTERS table(Script given below) and expect the following result:
1. When I pass a value of 0 for ID, It returns all the rows given in the table.
2. When I pass a value other than 0, It returns the row for the given value as well as all its child records.
CCENTER has parent-child relationship in ID and BASE column. I am using a query with DECODE function. but DECODE function in WHERE clause is not capable of handling sub-query with multiple rows.
VARIABLE ParaCCenter NUMBER
BEGIN
:paraccenter:=0;
END;
CREATE TABLE ccenters
(id NUMBER,
name VARCHAR2(20),
base number);
INSERT INTO ccenters VALUES(1,'NUST',null);
INSERT INTO ccenters VALUES(2,'SEECS',1);
INSERT INTO ccenters VALUES(3,'NBS',1);
commit;
SELECT * FROM ccenters
WHERE id IN DECODE(:ParaCCenter, 0, id,
(SELECT id FROM ccenters
START WITH base=:ParaCCenter
CONNECT BY PRIOR id = base
UNION
SELECT :ParaCCenter FROM dual
BEGIN
:paraCCenter:=1;
END;
SELECT * FROM ccenters
WHERE id IN DECODE(:ParaCCenter, 0, id,
(SELECT id FROM ccenters
START WITH base=:ParaCCenter
CONNECT BY PRIOR id = base
UNION
SELECT :ParaCCenter FROM dual))
The result is
(SELECT id FROM ccenters
ERROR at line 3:
ORA-01427: single-row subquery returns more than one row
How this query can be rewritten for the given functionality. Any response will be highly appreciated.
Thanks

And if you want to use DECODE:
SQL> BEGIN
  2  :paraccenter:=0;
  3  END;
  4  /
PL/SQL procedure successfully completed.
SQL> select  *
  2    from  ccenters
  3    where :paraccenter = decode(:paraccenter,0,0,id)
  4  /
        ID NAME                       BASE
         1 NUST
         2 SEECS                         1
         3 NBS                           1
SQL> BEGIN
  2  :paraccenter:=2;
  3  END;
  4  /
PL/SQL procedure successfully completed.
SQL> select  *
  2    from  ccenters
  3    where :paraccenter = decode(:paraccenter,0,0,id)
  4  /
        ID NAME                       BASE
         2 SEECS                         1
SQL> SY.

Similar Messages

  • When subquery returns multiple rows

    I have a doubt in this case. I follow Oracle SQL by example book and I find that "<=" is used in this query.
    He says that when the subquery returns single row,,only then <=, >=, = should be used..
    Why did they not use ANY,ALL or SOME operators.....When I use ANY,ALL ,,,it gives me an error "Invalid Relational operator"...... Please help
    select c.description, s.section_no, c.cost, s.capacity
    from course c, section s
    where c.course_no=s.course_no
    and s.capacity <= (select avg(capacity) from section)
    and c.cost=(select min(cost) from course)

    Hi,
    user11090588 wrote:
    I have a doubt in this case. I follow Oracle SQL by example book and I find that "<=" is used in this query.
    He says that when the subquery returns single row,,only then <=, >=, = should be used..
    Why did they not use ANY,ALL or SOME operators.....If the sub-query returns no more than 1 row, then it doesn't matter which, if any, of the keywords ANY, ALL or SOME you use: they all give the same results. Only when there are 2 or more rows can it matter if you're doing the comparison to any, all or some of the rows.
    When I use ANY,ALL ,,,it gives me an error "Invalid Relational operator"...... Please help
    select c.description, s.section_no, c.cost, s.capacity
    from course c, section s
    where c.course_no=s.course_no
    and s.capacity <= (select avg(capacity) from section)
    and c.cost=(select min(cost) from course)If the problem occurs when you use ANY or ALL, why not post a query where you use ANY or ALL?
    Post a complete script that people can use to re-create the problem and test their ideas. That includes CREATE TABLE and INSERT statements for your tables (unless you can show the problem using commonly available tables, like those in the scott schema).
    Always say which version of Oracle you're using.
    I can't reproduce the problem. I don't get any error using ANY, ALL, SOME or nothing before either sub-query:
    SELECT     d.deptno
    ,     e.ename
    ,     e.sal
    FROM     scott.dept     d
    JOIN     scott.emp     e  ON     d.deptno     = e.deptno
    WHERE     e.sal  <= ANY (
                   SELECT  AVG (sal)
                   FROM     scott.emp
    AND     d.deptno = SOME (
                       SELECT  MIN (deptno)
                       FROM    scott.dept
    ;Output from the query above:
    `   DEPTNO ENAME             SAL
            10 MILLER           1300I'm using Oracle 10.2.0.1.0.
    Don't worry too much about ANY, ALL or SOME before sub-queries. I've never seen them used outside of a textbook (or questions like this, taken from a textbook). In real life, nobody uses them.
    Someone once showed me any example where one of these actually would be useful if you didn't have analytic functions, but I didn't note what it was, or where I saw it. Analytic functions were introduced in Oracle 8.1, so that's only of historical interest now.

  • Case Working in "TOAD" but not Working in "WHERE" clause

    Hi Friends,
    When I am using case in the where clause of SQL query in Oracle Reports 3.0.5, than it is giving me "Error - Rep - 0946 Unable to Parse Query"
    But when I am running the same query in TOAD it is working fine.
    select *from inv_stores, inv_product, inv_location
    where  (inv_stores.location_code in ('12', '13', '16', '17', '18') and
                inv_stores.location_code = inv_location.location_code and
                inv_stores.department_code = inv_product.department_code and
                inv_stores.product_code = inv_product.product_code and
                nvl(delete_flag, 'N') <> 'Y' and
                inv_product.department_code = '11' and
                       (((CASE WHEN nvl(inv_product.sale_price3jd,0) = 0 THEN 0  ELSE (nvl(inv_product.sale_price3jd,0) - nvl(inv_product.cost_pricejd,0)) /
                       nvl(inv_product.sale_price3jd,0) END) *  100 ) <= 100))
    order by inv_product.department_code, inv_product.product_codePlease Help me.......
    Thanks in Advance

    Hello,
    When you create a SQL query in Reports, Reports will parse the SQL query in order to create the "data model".
    Reports 3.0.5 is quite old, so, you can use only the SQL "supported" by the RSF included in Reports 3.0.5.
    When you use TOAD , the sql query is not parsed by TOAD but by the DB just like with sqlplus or SQL Developer.
    Regards

  • HT1414 touch screen does not work on my ipod when playing music on itunes, but works fine on every other thing on ipod

    touch screen does not work on my ipod when playing music on itunes, but works fine on every other thing on ipod, can anyone help or what is the cause

    Try:
    - Reset the iOS device. Nothing will be lost
    Reset iOS device: Hold down the On/Off button and the Home button at the same time for at
    least ten seconds, until the Apple logo appears.
    - Unsync all music and resync
    - Reset all settings
    Go to Settings > General > Reset and tap Reset All Settings.
    All your preferences and settings are reset. Information (such as contacts and calendars) and media (such as songs and videos) aren’t affected.
    - Restore from backup. See:                                 
    iOS: How to back up           
    - Restore to factory settings/new iOS device.
    If still problem, make an appointment at the Genius Bar of an Apple store since it appears you have a hardware problem.
    Apple Retail Store - Genius Bar          

  • CaptureDeviceManager.getDeviceList  is not working in Windows VISTA.When

    CaptureDeviceManager.getDeviceList is not working in Windows VISTA.When I run the Application from vista using a jar file.But it is running if i run it from class file without use the jar file.

    This pretty much says it all:
    No connection could be made because the target machine actively refused it.What version of Vista?
    What version of the agent? ... 10g is not a version number it is a marketing label
    Did you check to see if the agent is compatible with your operating system?
    My recommendation would be to get a real operating system: Either Oracle Linux or XP.

  • My youtube does not work on safari only when i try to play a video it says "This video is not available on mobile add to playlist" But i am using a mac air 11 inch it is not a mobile. Please Help. i have to use the youtube on firefox or chrome

    my youtube does not work on safari only when i try to play a video it says "This video is not available on mobile add to playlist" But i am using a mac air 11 inch it is not a mobile. Please Help. i have to use the youtube on firefox or chrome. At times it also say QuickTime Player can't open "video.3gp". and
    The file may be damaged or may not be a movie file that is compatible with QuickTime Player. when i try to open a video on youtube on safari

    I too am having the same issue as the OP.
    Your USER AGENT information is Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/600.3.18 (KHTML, like Gecko) Version/8.0.3 Safari/600.3.18
    Every webserver that receives a request from your browser is able to determine the HTTP USER AGENT information unless it has been removed by some software (e.g. firewall) before the request was trasmitted.

  • I need to go back to Firefox 3.5-3.6 because 90% of my extension do not work. Where can I find the download to Firefox 3.5-3.6? Thank you for the link.

    I need to go back to Firefox 3.5-3.6 because 90% of my extension do not work. Where can I find the download to Firefox 3.5-3.6? Thank you for the link.

    You are a "life saver". Thank you very much.

  • [svn] 1720: Bugs: LCDS-304 - Authentication not working in all cases when using security constraint with NIO endpoints .

    Revision: 1720
    Author: [email protected]
    Date: 2008-05-14 14:50:06 -0700 (Wed, 14 May 2008)
    Log Message:
    Bugs: LCDS-304 - Authentication not working in all cases when using security constraint with NIO endpoints.
    QA: Yes
    Doc: No
    Details:
    Update to the TomcatLoginCommand to work correctly with NIO endpoints.
    Ticket Links:
    http://bugs.adobe.com/jira/browse/LCDS-304
    Modified Paths:
    blazeds/branches/3.0.x/modules/opt/src/tomcat/flex/messaging/security/TomcatLoginCommand. java

    Revision: 1720
    Author: [email protected]
    Date: 2008-05-14 14:50:06 -0700 (Wed, 14 May 2008)
    Log Message:
    Bugs: LCDS-304 - Authentication not working in all cases when using security constraint with NIO endpoints.
    QA: Yes
    Doc: No
    Details:
    Update to the TomcatLoginCommand to work correctly with NIO endpoints.
    Ticket Links:
    http://bugs.adobe.com/jira/browse/LCDS-304
    Modified Paths:
    blazeds/branches/3.0.x/modules/opt/src/tomcat/flex/messaging/security/TomcatLoginCommand. java

  • I have MacBookpro with OSX 10.9, and my Tata Photon plus  is not working on it. When I try to connect, Its not connecting.. Please Help

    I have a MacBookPro with OSX 10.9, and my Tata Photon plus  is not working on it. When I try to connect, Its not connecting.. Please Help
    <Email Edited by Host>

    Current Firefox versions require a Mac with an Intel processor and OS X 10.6 (Firefox 16 runs on Intel Mac OS X 10.5).
    *http://www.mozilla.org/firefox/25.0/system-requirements/
    For an unofficial Firefox 17.0.10 ESR compatible version that runs on a PowerPC Mac with OS X 10.4.11 or OS X 10.5.8 you can look at TenFourFox.
    *http://www.floodgap.com/software/tenfourfox/
    *http://www.macupdate.com/app/mac/37761/tenfourfox
    *http://code.google.com/p/tenfourfox/wiki/ReleaseNotes1710
    See also:
    *http://code.google.com/p/tenfourfox/wiki/PluginsNoLongerSupported
    *http://code.google.com/p/tenfourfox/wiki/QuickTimeEnabler
    *http://code.google.com/p/tenfourfox/wiki/AAATheFAQ

  • I buy macbook pro occsaion ; i download osx 10.8.2 and reintal osx , after 2 days  my keyboard is not working correctly ( i mean when i touch the keys ,i see the symles , icant get letters when i type .  i try externel keyboard but is work

    i buy macbook pro occsaion ; i download osx 10.8.2 from internet  and reintal osx , after 2 days  my keyboard is not working correctly ( i mean when i touch the keys ,i see the symles , i cant get letters when i type .  i try with externel keyboard but is work

    Thanks for the reply but there is no option tab or wake for administration use in mountain lion 10.8.2, I only have the wake for network access which is on, I didnt used to have this problem before snow leopard now I have to manually re-login after computer wakes and then access remotely ........

  • Recently Iam purchased a Iphone from U.S, but iam residing in India, so can any body tell me in india how can i get support as my phone charger is not working from where in india i can replace i

    Recently Iam purchased a Iphone from U.S, but iam residing in India, so can any body tell me in india how can i get support as my phone charger is not working from where in india i can replace it

    gosharma wrote:
    What if we have Applecare? Applecare is worldwide. Isnt it?
    Not on an iPhone.

  • The microphone of my iphone is not working on call but when i use viber it is working

    The microphone of my iphone is not working on calls but when i use viber it is working

    Hello there, qasmicomputers.
    The following Knowledge Base article provides some great troubleshooting steps for your issue:
    iPhone: Microphone issues
    http://support.apple.com/kb/ts5183
    Thanks for reaching out to Apple Support Communities.
    Cheers,
    Pedro.

  • Microphone not working on a call when cellular data is on...any help..ty..

    microphone not working on a call when cellular data is on...The person at the other end of the call cant hear me and when i turn it off it works fine..

    Hi there ravitejanvr,
    You may find the troubleshooting steps in the article below helpful.
    iPhone: Microphone issues
    http://support.apple.com/kb/ts5183
    -Griff W. 

  • Why do hyperlinks not work in a pdf when it is signed?

    Why do hyperlinks not work in a pdf when it is signed?

    Adobe reader there any payment to be no problems or allow sharing through hyperlinks.
    Dentistas en lima

  • HT1202 The manual mode to transfer music to my iTune from my iPhone does not work as described. When I click on Manually manage... it displays the warning message that all music on my iPhone will be erased and replaced by iTune music.

    The manual mode to transfer music to my iTune from my iPhone does not work as described. When I click on Manually manage... it displays the warning message that all music on my iPhone will be erased and replaced by iTune music.
    How can I transfer my music to a second computer iTune?
    Thanks,

    See this user tip
    Syncing iPhone to a "New" Computer or replacing a "crashed" Hard Drive

Maybe you are looking for