0

This is a homework exercise in pl/sql. I have to sort the values within each row in a table, whose columns should be retrieved from user_tab_columns. I'm a very beginner in pl/sql. I've tried for many hours from different approaches without solving it correctly.

The code below may seem stupid, but please, can you help me?

 CREATE TABLE notat(Id Number, Col1 Number, Col2 Number, Col3 Number,   Col4 Number, Col5 Number, Col6 Number, Col7 Number);
 INSERT INTO notat VALUES(1,6,3,8,4,7,8,4);
 INSERT INTO notat VALUES(2,5,7,9,2,1,7,8);
 INSERT INTO notat VALUES(3,2,7,4,8,1,5,9);
 INSERT INTO notat VALUES(4,8,4,7,9,4,1,4);
 INSERT INTO notat VALUES(5,7,5,2,5,2,6,4);

 create or replace procedure sp1_fy_marks is
    v_colname  varchar2(10);
    v2_colname varchar2(10);
    tmp        number;
    tmp2       number;

 begin

 for row in (select * from notat)
 loop

 for x in (select column_name
            from user_tab_columns
           where table_name = 'NOTAT')
 loop

  v_colname := x.column_name;

  execute immediate 'Select ' || v_colname || ' from   NOTAT where Id='  || row.Id into tmp;

  for y in (select column_name
              from user_tab_columns
             where table_name = 'NOTAT')
  loop

    v2_colname := y.column_name;

    execute immediate 'Select ' || v2_colname || ' from NOTAT where Id=' || row.Id into tmp2;

    if tmp > tmp2 then
      execute immediate 'UPDATE NOTAT SET ' || v_colname || '=' || tmp2 || '  WHERE Id=' || row.Id;
      execute immediate 'UPDATE NOTAT SET ' || v2_colname || '=' || tmp || '  WHERE Id=' || row.Id;
    end if;

  end loop;
  end loop;
  end loop;
  end sp1_fy_marks;
  execute sp1_fy_marks;
4
  • could me please explain it clearly...not getting what ur trying to do actually
    – theDbGuy
    Apr 17, 2015 at 9:59
  • I'm trying to sort the values that reside in different columns of the same row. Apr 17, 2015 at 10:04
  • That's a pretty silly thing to do, if you ask me. Are you sure that's what the exercise is really about? Apr 17, 2015 at 10:08
  • I'm pretty sure, but I know it's sounds silly... Apr 17, 2015 at 10:15

2 Answers 2

0

As I say that seems like a silly requirement - I've spent 35 years using db's and never even thought of doing something like that.

However, your code looks like the correct approach. Not being current with PL/SQL (it's a few years since I last used it) I haven't checked the syntax or anything, but the approach is what I think I'd take if I had to do it.

However if I did have to do it, I would actually argue that the database is incorrectly designed. Columns should contain separate types of data, and thus not be comparable. If they are logically comparable then you have a break of 3NF - you have a repeating group. The correct design would break the columns out as rows in a differently designed table. Sorting is then a trivial exercise. This is sort-of what you're doing is your proposed solution.

So I'd say: Yes, given a pretty silly thing to do you're doing the least stupid thing possible in the circumstances!

0

Please..., format your code, many simple problems are easily seen and fixed if the code is formatted. As a side note, I strongly suggest that you get in a habit of prepend your table names with the schema especially when referring to tables outside the owners schema, so I would suggest sys.user_tab_columns, even if not technically necessary.

create or replace procedure sp1_fy_marks is
  v_colname  varchar2(10);
  v2_colname varchar2(10);
  tmp        number;
  tmp2       number;
begin
  for row in (select * from notat)
  loop

    for x in (select column_name
                from user_tab_columns
               where table_name = 'NOTAT')
    loop

      v_colname := x.column_name;

      execute immediate 'Select ' || v_colname || ' from   NOTAT where Id=' || row.Id into tmp;

      for y in (select column_name
                  from user_tab_columns
                 where table_name = 'NOTAT')
      loop

        v2_colname := y.column_name;

        execute immediate 'Select ' || v2_colname || ' from NOTAT where Id=' || row.Id into tmp2;

        if tmp > tmp2 then
          execute immediate 'UPDATE NOTAT SET ' || v_colname || '=' || tmp2 || '  WHERE Id=' || row.Id;
          execute immediate 'UPDATE NOTAT SET ' || v2_colname || '=' || tmp || '  WHERE Id=' || row.Id;
        end if;

      end loop;
    end loop;
  end loop;
end sp1_fy_marks;
execute sp1_fy_marks;

Second, EVERY table is kind of like a bucket, data is poured in and while in the bucket there is no order imposed. The selection of data out of the bucket imposes an order. This includes the selection of data from sys.user_tab_columns. You have two selects from sys.user_tab_columns, there is no assurance that the rows containing the column names are in the same order at all.

Third, You are assuming an order of the columns, probably the order that occurs with select * from notat, if you have columns a b c in notat, you could 'select b c a from notat' or 'select c a b from notat' Also, select * from sys.user_tab_columns with some imposed order does not ensure the same order of the select * from notat

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.