PL/SQL theoretical questions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kiss07
    Banned
    New Member
    • Jan 2007
    • 99

    #1

    PL/SQL theoretical questions

    Hi,

    i want 2 and 3 rd max salary in a table .i want a single query.

    please help..

    Arun
  • mailtothiyagarajan
    New Member
    • Apr 2007
    • 1

    #2
    Originally posted by kiss07
    Hi,

    i want 2 and 3 rd max salary in a table .i want a single query.

    please help..

    Arun
    I have used the inline view concept for this


    Query for nth max salary :

    select s.salary from
    (
    (
    select dense_rank() over (order by max(salary) desc) n,salary
    from
    employee
    ) s
    )
    where s.n=&n;

    Comment

    • bonzi1405
      New Member
      • May 2007
      • 5

      #3
      Below will return n,n+1 higest salary of a table.


      select min(salary) sal
      from
      ( select salary from employee order by salary desc)
      where rownum<=&n
      union
      select min(salary) sal
      from
      ( select salary from employee order by salary desc)
      where rownum<=&n+1

      Comment

      • debasisdas
        Recognized Expert Expert
        • Dec 2006
        • 8119

        #4
        Dear
        mailtothiyagara jan

        have u tested the code before posting

        Comment

        • Mani kansal
          New Member
          • Apr 2007
          • 11

          #5
          select sal from (select sal from emp order by sal desc) where rownum in(2,3)

          Comment

          • kiss07
            Banned
            New Member
            • Jan 2007
            • 99

            #6
            Exceptions

            Dear friends,

            I known about pre defined exception:
            i want a sample programs in each of predefined exception.

            example:

            invalid cursor :

            one sample program. when exception is raised in ivvalid cursor
            same as others exceptions...


            pls anybody help....

            arun..

            Comment

            • kiss07
              Banned
              New Member
              • Jan 2007
              • 99

              #7
              sql Tunings

              Dear debas,

              Can u explain about sql tuning:

              what are the steps are doing sql tuning . I want to know about basic ideas

              in sql tuning and performance tuning. what are the types?..

              Pls...

              Arunkumar...

              Comment

              • debasisdas
                Recognized Expert Expert
                • Dec 2006
                • 8119

                #8
                PLease follow this Link

                Hope it might be useful for u.

                Comment

                • debasisdas
                  Recognized Expert Expert
                  • Dec 2006
                  • 8119

                  #9
                  Invalid cursor exception is raised when u try to do somthing which is not allowed .
                  for ex

                  accesing the cursor variable after the cursor is closed.

                  Comment

                  • debasisdas
                    Recognized Expert Expert
                    • Dec 2006
                    • 8119

                    #10
                    ZERO_DIVIDE exception
                    =============== ========
                    Code:
                    DECLARE
                    I INT:=&I;
                    J INT:=&J;
                    K INT;
                    BEGIN
                    K:=I/J;
                    DBMS_OUTPUT.PUT_LINE(K);
                    EXCEPTION
                    WHEN ZERO_DIVIDE THEN
                    RAISE_APPLICATION_ERROR(-20002,'CANT DIVIDE BY ZERO.....!');
                    WHEN OTHERS THEN
                    RAISE_APPLICATION_ERROR(-20003,'SOME OTHER ERROR........!');
                    END;

                    Comment

                    • debasisdas
                      Recognized Expert Expert
                      • Dec 2006
                      • 8119

                      #11
                      A sample example of
                      NO_DATA_FOUND exception
                      =============== =============
                      Code:
                      DECLARE
                      NAME VARCHAR2(20);
                      NO INT:=&NO;
                      BEGIN
                      SELECT ENAME INTO NAME FROM EMP WHERE EMPNO=NO;
                      DBMS_OUTPUT.PUT_LINE(NAME);
                      EXCEPTION
                      WHEN NO_DATA_FOUND THEN
                      RAISE_APPLICATION_ERROR(-20002,'NO DATA IS FOUND FOR THIS RECORD.....!');
                      WHEN OTHERS THEN
                      RAISE_APPLICATION_ERROR(-20003,'SOME OTHER ERROR........!');
                      END;

                      Comment

                      • debasisdas
                        Recognized Expert Expert
                        • Dec 2006
                        • 8119

                        #12
                        A sample example of
                        INVALID_CURSOR exception
                        =============== ========
                        Code:
                        DECLARE
                        CURSOR c1 IS SELECT ename FROM emp WHERE ROWNUM < 11;
                        name emp.ename%TYPE;
                        BEGIN
                        OPEN c1;
                        LOOP
                        FETCH c1 INTO name;
                        EXIT WHEN c1%NOTFOUND;
                        dbms_output.put_line(c1%ROWCOUNT || '. ' || name);
                        END LOOP;
                        CLOSE c1;
                        dbms_output.put_line(c1%ROWCOUNT); 
                        exception
                        when invalid_cursor then
                        raise_application_error(-20001,'invalid operation in cursor');
                        END;
                        In the above example the cursor is invalid because of trying to access the cursor attribute %rowcount after the cursor is closed(in 2nd dbms_output ......). This raises INVALID_CURSOR exception.

                        Comment

                        • kiss07
                          Banned
                          New Member
                          • Jan 2007
                          • 99

                          #13
                          Dear Debasisdas,

                          Am really proud of Your answer. Thanku Very much.

                          Regards,
                          Arun..

                          Comment

                          • kiss07
                            Banned
                            New Member
                            • Jan 2007
                            • 99

                            #14
                            Error handling

                            dear friends,

                            I have some doubt .
                            This is sample program

                            DECLARE
                            ---
                            BEGIN
                            SELECT employee_id, last_name, salary INTO emp_id, emp_lastname,
                            emp_salary FROM employees WHERE employee_id = 120;
                            UPDATE emp_name SET salary = salary * 1.1 WHERE employee_id = emp_id;
                            DELETE FROM emp_name WHERE employee_id = 130;
                            INSERT INTO emp_name VALUES (emp_id, emp_lastname, emp_salary);

                            --SOME DML STATEMENTS
                            --this is sample . but contain lot of statemnts are there in this body.
                            Delete .............
                            insert ...............
                            select ............... ...

                            END;


                            i got an error .i don't known which statement.
                            How can i find out which statement is got an error.How can i handle this error statement..

                            Pls explain briefly........ ...


                            Arunkumar

                            Comment

                            • debasisdas
                              Recognized Expert Expert
                              • Dec 2006
                              • 8119

                              #15
                              If u r using sql* plus

                              To solve this better use any tool like TOAD or PLSQL DEVELOPER.

                              Since u r new to pl/sql it will be difficult for you to find the bug using sql* plus.

                              Comment

                              Working...