You are on page 1of 20

LOGIC WORKS 01 AUTHOR JP VIJAYKUMAR CASE LOGIC 01 DATE 11-14-09 ************* --PROBLEM_DEFINITION: --I WANT ALL THE TABLES

WITH PRIMARY KEYS DEFINED ON MULTIPLE COLUMNS --(PRIMARY KEY INDEXES CREATED ON TWO COLUMN ONLY) --THAT ARE HAVING SAL COLUMN INCLUDED. FIND WHETHER DUPLICATE ROWS ARE --EXISTING IN TABLES ON THE REMAINING COLUMN, IF SAL COLUMN IS REMOVED --FROM THE PRIMARY KEY INDEXED COLUMNS --TEST CASE SCENARIO create user jp identified by jp account unlock; grant create session to jp; alter user jp default tablespace users; alter user jp quota unlimited on users; create table jp.temp_jp1(empno number, ename varchar2(20),sal number); create table jp.temp_jp2(customerno number,custname varchar2(20), sal number); insert into jp.temp_jp1 values (1,'HANUMAN',1); insert into jp.temp_jp1 values (1,'HAHNEMANN',2); insert into jp.temp_jp2 values (1,'ANJANEYA',1); insert into jp.temp_jp2 values (1,'SUNDARAM',2); commit; alter table jp.temp_jp1 add constraint temp_jp1_pk primary key (empno,sal); alter table jp.temp_jp2 add constraint temp_jp2_pk primary key (customerno,sal); set serverout on size 1000000 declare v_num number; v_sql varchar2(1000); begin for c1 in ( select column_name, index_name,table_name from dba_ind_columns where index_owner='JP' and index_name in (select index_name from ( select index_name,count(*) from dba_ind_columns where index_owner='JP' and index_name in (select constraint_name from dba_constraints where owner='JP' and constraint_type='P') group by index_name having count(*) > 1)) and column_name not in ('SAL')) loop begin v_sql:='select count(*) from jp.' c1.table_name ' group by ' c1.column_name

' having count(*) > 1'; execute immediate v_sql into v_num; if (v_num > 0) then dbms_output.put_line(c1.column_name ' ' c1.index_name ' ' c1.table_name ' ' v_num); end if; exception when others then dbms_output.put_line(c1.column_name ' ' c1.index_name ' ' c1.table_name ' ' sqlerrm); end; end loop; end; / EMPNO TEMP_JP1_PK TEMP_JP1 2 CUSTOMERNO TEMP_JP2_PK TEMP_JP2 2 PL/SQL procedure successfully completed. --THIS SCRIPT WILL NOT WORK IF THE PRIMARY KEY INDEXES ARE CREATED ON > TWO COLU MNS --A DIFFERENT STRATEGY IS REQUIRED TO HANDLE INDEXES WITH MORE THAN TWO COLUMNS References: http://www.rocket99.com/techref/oracle8614.html ----------------------------------------------------------CASE LOGIC 02 DATE 11-22-09 ************* --PROBLEM_DEFINITION: --IN OUR APPLICATION MANY TABLES' FOREIGN KEY CONSTRAINTS REFERRENCING EMP TABLE --IN EMP TABLE PRIMARY KEY COLUMN IS DEFINED ON EMPNO. DUPLICATES EXISTED IN EMP TABLE ON --ENAME COLUMN. YOU NEED TO DELETE DUPLICATE RECORDS FROM EMP TABLE AND UPDATE T HE --REFERENCING KEY VALUES IN THE CHILD TABLES. YOU NEED TO UPDATE THE EMPNO IN AL L THE --CHILD TABLES TO THE RECORD RETAINED IN THE EMP TABLE. --TEST CASE SCENARIO create table jp.empl_jp(emplno number, ename varchar2(10)); create table jp.proj_jp(deptno number, emplno number,dname varchar2(10)); create table jp.wage_jp(wageno number, emplno number, sal number); truncate table jp.empl_jp; truncate table jp.proj_jp; truncate table jp.wage_jp; insert insert insert insert into into into into jp.empl_jp jp.empl_jp jp.empl_jp jp.empl_jp values(1,'RAMA'); values(2,'RAMA'); values(3,'RAMA'); values(4,'SITA');

insert into jp.empl_jp values(5,'SITA'); insert into jp.empl_jp values(6,'BABA'); insert insert insert insert insert insert insert insert insert insert insert insert insert insert insert insert insert into into into into into into into into into into into into into into into into into jp.proj_jp jp.proj_jp jp.proj_jp jp.proj_jp jp.proj_jp jp.proj_jp jp.proj_jp jp.proj_jp jp.wage_jp jp.wage_jp jp.wage_jp jp.wage_jp jp.wage_jp jp.wage_jp jp.wage_jp jp.wage_jp jp.wage_jp values(1,1,'SALES'); values(2,2,'MARKETING'); values(3,3,'RESEARCH'); values(4,3,'TRAVEL'); values(5,4,'ACCOUNTS'); values(6,5,'HOUSING'); values(7,6,'BILLS'); values(8,6,'TRACKING'); values(1,1,100); values(2,2,100); values(3,2,100); values(4,3,100); values(5,4,100); values(6,4,100); values(7,5,100); values(8,5,100); values(9,6,100);

set serverout on size 1000000 declare begin for c1 in ( select emplno,ename, (select min(emplno) from jp.empl_jp where ename = a.ename group by ename) min_emplno from jp.empl_jp a where ename in (select ename from ( select ename, count(*) from jp.empl_jp group by ename having count(*) > 1))) loop begin if (c1.emplno > c1.min_emplno) then update jp.proj_jp set emplno = c1.min_emplno where emplno = c1.emplno; update jp.wage_jp set emplno = c1.min_emplno where emplno = c1.emplno; delete jp.empl_jp where emplno = c1.emplno and ename = c1.ename; commit; end if; exception when others then dbms_output.put_line(c1.emplno ' ' c1.ename ' ' c1.min_emplno); end; end loop; end; Acknowledgements: Thanks Vineela, for designing the test case and scripting. ----------------------------------------------------------CASE LOGIC 03 DATE 12-07-09 ************* --PROBLEM_DEFINITION:

--THE VALUES IN ONE REPORT, GENERATED IN COLUMNS, ARE TO BE DISPLAYED IN ROWS. --ROTATING THE OUTPUT OF THE REPORT BY 90 DEGREES --TEST CASE SCENARIO create table temp_jp(airportcode char(3),trandate date,traveller_count number); insert insert insert insert insert insert insert insert insert into into into into into into into into into temp_jp temp_jp temp_jp temp_jp temp_jp temp_jp temp_jp temp_jp temp_jp values('DFW',SYSDATE -6,10); values('DFW',SYSDATE -4,22); values('DFW',SYSDATE,11); values('LVF',SYSDATE-5,6); values('LVF',SYSDATE-3,2); values('LVF',SYSDATE-2,4); values('HBH',SYSDATE -1,8); values('HBH',SYSDATE -6,9); values('HBH',SYSDATE -4,5);

select * from temp_jp; AIR --DFW DFW DFW LVF LVF LVF HBH HBH HBH TRANDATE TRAVELLER_COUNT --------- --------------24-NOV-09 10 26-NOV-09 22 30-NOV-09 11 25-NOV-09 6 27-NOV-09 2 28-NOV-09 4 29-NOV-09 8 24-NOV-09 9 26-NOV-09 5

9 rows selected. --HERE IN THE ABOVE REPORT THE TRAVELLER_COUNT IS TO BE DISPLAYED IN ROWS --UNDER THE COLUMN_NAME OF THE TRANDATE. --AND THE FOLLOWING SQL QUERY DOES THE REQUIRED WORK. SQL> 9", 2 3 4 5 6 7 8 9 select airportcode,decode(trandate,'24-NOV-09',traveller_count,0) "24-NOV-0 decode(trandate,'25-NOV-09',traveller_count,0) "25-NOV-09", decode(trandate,'26-NOV-09',traveller_count,0) "26-NOV-09", decode(trandate,'27-NOV-09',traveller_count,0) "27-NOV-09", decode(trandate,'28-NOV-09',traveller_count,0) "28-NOV-09", decode(trandate,'29-NOV-09',traveller_count,0) "29-NOV-09", decode(trandate,'30-NOV-09',traveller_count,0) "30-NOV-09" from temp_jp where airportcode in ('DFW','LVF','HBH') /

AIR 24-NOV-09 25-NOV-09 26-NOV-09 27-NOV-09 28-NOV-09 29-NOV-09 30-NOV-09 --- ---------- ---------- ---------- ---------- ---------- ---------- ---------DFW 10 0 0 0 0 0 0 DFW 0 0 22 0 0 0 0 DFW 0 0 0 0 0 0 11 LVF 0 6 0 0 0 0 0 LVF 0 0 0 2 0 0 0 LVF 0 0 0 0 4 0 0 HBH 0 0 0 0 0 8 0 HBH 9 0 0 0 0 0 0 HBH 0 0 5 0 0 0 0

9 rows selected. SQL> select airportcode,sum(decode(trandate,'24-NOV-09',traveller_count,0)) "24NOV-09", 2 sum(decode(trandate,'25-NOV-09',traveller_count,0)) "25-NOV-09", 3 sum(decode(trandate,'26-NOV-09',traveller_count,0)) "26-NOV-09", 4 sum(decode(trandate,'27-NOV-09',traveller_count,0)) "27-NOV-09", 5 sum(decode(trandate,'28-NOV-09',traveller_count,0)) "28-NOV-09", 6 sum(decode(trandate,'29-NOV-09',traveller_count,0)) "29-NOV-09", 7 sum(decode(trandate,'30-NOV-09',traveller_count,0)) "30-NOV-09" 8 from temp_jp where airportcode in ('DFW','LVF','HBH') group by airportcode order by 1 9 / AIR 24-NOV-09 25-NOV-09 26-NOV-09 27-NOV-09 28-NOV-09 29-NOV-09 30-NOV-09 --- ---------- ---------- ---------- ---------- ---------- ---------- ---------DFW 10 0 22 0 0 0 11 HBH 9 0 5 0 0 8 0 LVF 0 6 0 2 4 0 0 --THE ABOVE RESULT IS PRODUCED BY THE FOLLOWING PL/SQL PROCEDURE ALSO. set serverout on size 1000000 set linesize 200 declare v_str1 varchar2(1000):='AIRPORTCODE'; v_str2 varchar2(1000); v_temp number; v_date date; begin for j in reverse 1..7 loop v_date:=(sysdate - j); v_str1:=v_str1 ' ' to_char(v_date,'mm-dd-yy'); end loop; dbms_output.put_line(v_str1); for c1 in (select distinct airportcode airportcode from temp_jp order by airport code) loop v_str2:=c1.airportcode; for i in reverse 1..7 loop v_date:=(sysdate - i); execute immediate 'select sum(decode(trandate,''' v_date ''',traveller_count,0)) from temp_jp where airportcode=''' c1 .airportcode ''' ' into v_temp; v_str2:=v_str2 ' ' v_temp; end loop; dbms_output.put_line(v_str2); end loop; end; References: http://www.dbasupport.com/oracle/ora10g/RotatingTable.shtml ----------------------------------------------------------CASE LOGIC 04 DATE 12-14-09

************* --PROBLEM_DEFINITION: --I WANT TO FIND THE NUMBER OF TABLES, STARTING WITH EACH UPPER CASE ALPHABET IN MY SCHEMA. --TEST CASE SCENARIO select alphabet, (select count(1) from dba_tables where owner='SCOTT' and substr(table_name,1,1) = a.alphabet) table_count from (select distinct(substr(table_name,1,1)) alphabet from dba_tables where owner=' SCOTT' ) a order by alphabet; --THE SAME RESULT IS PRODUCED WITH THE FOLLOWING PL/SQL PROCEDURE set serverout on size 1000000 declare v_str char(1); v_num number; i number:=1; begin while (i<=26) loop begin select substr('ABCDEFGHIJKLMNOPQRSTUVWXYZ',i,1) into v_str from dual; execute immediate 'select count(1) from dba_tables where table_name like ''' v_str '%' ''' and owner=''' 'SCOTT' ''' ' into v_num; dbms_output.put_line(v_str ' ' v_num); i:=i+1; exception when others then dbms_output.put_line(v_str ' ' v_num ' ' sqlerrm); end; end loop; end; ----------------------------------------------------------CASE LOGIC 05 DATE 01-04-10 ************* PROBLEM DEFINITION --FOR EACH CODE IN THE FLAT FILE, THERE ARE VALUES. --THE BUSINESS WANTS TO DISPLAY ONLY THE NON-ZERO VALUES IN ASCENDING ORDER. --FROM THE DISPLAYED RECORDS DISCARD THE FIRST 1/3 RD RECORDS AND LAST 1/3 RD RE CORDS. --FIND THE AVERAGE OF THE REMAINING MIDDLE 1/3 RD RECORDS ONLY FOR EACH CODE. $ cat Assesment.txt 100 0 0 0 0 0 0 0 0 0 0 55474 0 0 0 47769 0 95915 0 0 61401 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

111 0 0 101 0 0 104 0 0 114 105 106 0 107 108 109 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 41165 0 54744 0 0 0 55480 51781 0 0 54000 0 46481 0 0 64051 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 51804 0 55116 0 0 54100 0 0 0 0 0 0 0 0 0 0 0 0 57617 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 51781 0 49574 45448 0 0 0 0 49557 0 0 0 0 0 0 0 0 0 0 0 0 0 61711 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 49574 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 55141 0 0 0 0 0 0 40546 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 54015 0 0 0 64480 0 0 44104 54618 0 41449 0 0 48745 0 0 0 0 0

0 0 0 0 0 0 45844 0 0 0

create or replace directory data_pump_dir as '/u001/oracle/product/10.2.0/db_1/rdbms/log'; grant read,write on directory data_pump_dir to public; create code col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 col11 col12 col13 col14 col15 col16 col17 col18 col19 col20 col21 col22 col23 col24 col25 col26 col27 col28 col29 col30 col31 col32 col33 col34 col35 table xtern_temp_jp ( number(10), number(10), number(10), number(10), number(10), number(10), number(10), number(10), number(10), number(10), number(10), number(10), number(10), number(10), number(10), number(10), number(10), number(10), number(10), number(10), number(10), number(10), number(10), number(10), number(10), number(10), number(10), number(10), number(10), number(10), number(10), number(10), number(10), number(10), number(10), number(10)) organization external ( default directory data_pump_dir

access parameters ( records delimited by newline fields terminated by ' ' ) location ('Assesment.txt') ); create global temporary table counter_jp(col1 number) on commit delete rows; set serverout on size 1000000 declare v_ct1 v_ct2 v_sum v_avg begin for c1 in (select * from xtern_temp_jp) loop begin v_ct1 v_ct2 v_sum v_avg if end if; if end if; if end if; if end if; if end if; if end if; if end if; if (c1.col8 >0) then insert into counter_jp values(c1.col8); (c1.col7 >0) then insert into counter_jp values(c1.col7); (c1.col6 >0) then insert into counter_jp values(c1.col6); (c1.col5 >0) then insert into counter_jp values(c1.col5); (c1.col4 >0) then insert into counter_jp values(c1.col4); (c1.col3 >0) then insert into counter_jp values(c1.col3); (c1.col2 >0) then insert into counter_jp values(c1.col2); :=0; :=0; :=0; :=0; (c1.col1 >0) then insert into counter_jp values(c1.col1); number(10); number(10); number(10); number(10);

end if; if end if; if end if; if end if; if end if; if end if; if end if; if end if; if end if; if end if; if end if; if end if; if end if; if end if; if end if; if (c1.col23 >0) then insert into counter_jp values(c1.col23); (c1.col22 >0) then insert into counter_jp values(c1.col22); (c1.col21 >0) then insert into counter_jp values(c1.col21); (c1.col20 >0) then insert into counter_jp values(c1.col20); (c1.col19 >0) then insert into counter_jp values(c1.col19); (c1.col18 >0) then insert into counter_jp values(c1.col18); (c1.col17 >0) then insert into counter_jp values(c1.col17); (c1.col16 >0) then insert into counter_jp values(c1.col16); (c1.col15 >0) then insert into counter_jp values(c1.col15); (c1.col14 >0) then insert into counter_jp values(c1.col14); (c1.col13 >0) then insert into counter_jp values(c1.col13); (c1.col12 >0) then insert into counter_jp values(c1.col12); (c1.col11 >0) then insert into counter_jp values(c1.col11); (c1.col10 >0) then insert into counter_jp values(c1.col10); (c1.col9 >0) then insert into counter_jp values(c1.col9);

end if; if end if; if end if; if end if; if end if; if end if; if end if; if end if; if end if; if end if; if end if; if end if; if end if; execute immediate 'select count(1) from counter_jp' into v_ct1; if (v_ct1 > 0) then v_ct2:= trunc(v_ct1/3); dbms_output.put_line(c1.code ' ' v_ct1 ' ' v_ct2); execute immediate ' select sum(col1), avg(col1) from ( select rownum rnum , t1.* from (' ' select col1 from counter_jp order by col1) t1) t2 where t2.rnum >' (c1.col35 >0) then insert into counter_jp values(c1.col35); (c1.col34 >0) then insert into counter_jp values(c1.col34); (c1.col33 >0) then insert into counter_jp values(c1.col33); (c1.col32 >0) then insert into counter_jp values(c1.col32); (c1.col31 >0) then insert into counter_jp values(c1.col31); (c1.col30 >0) then insert into counter_jp values(c1.col30); (c1.col29 >0) then insert into counter_jp values(c1.col29); (c1.col28 >0) then insert into counter_jp values(c1.col28); (c1.col27 >0) then insert into counter_jp values(c1.col27); (c1.col26 >0) then insert into counter_jp values(c1.col26); (c1.col25 >0) then insert into counter_jp values(c1.col25); (c1.col24 >0) then insert into counter_jp values(c1.col24);

v_ct2 ' and t2.rnum <=(' v_ct1 ' - ' v_ct2 ') ' into v_sum, v_a vg; dbms_output.put_line(c1.code ' ' v_ct1 ' ' v_ct2 ' ' v_sum ' ' v_avg); dbms_output.put_line('-'); end if; commit; exception when others then dbms_output.put_line(c1.code ' ' sqlerrm); end; end loop; end; / 100 4 100 4 111 7 111 7 101 3 101 3 104 5 104 5 114 1 114 1 105 1 105 1 106 2 106 2 109 7 109 7 -

1 1 116875 58438 2 2 160525 53508 1 1 54100 54100 1 1 150912 50304 0 0 61711 61711 0 0 49574 49574 0 0 95687 47844 2 2 148604 49535

PL/SQL procedure successfully completed. References: http://www.adp-gmbh.ch/ora/misc/ext_table_2.html Acknowledgements: Thanks Vineela, for designing the test case and scripting. ----------------------------------------------------------CASE LOGIC 06 DATE 01-14-10 ************* --THIS PROCEDURE CAN CHECK, WHETHER THE WORD / SENTENSE IS A PALINDROME --'malayalam' --'eye'

--'saippuakivikauppias' Finnish word for soapstone vendor --'able was i saw elba' -----------------------------------------------------------------------set serverout on size 1000000 declare v_word varchar2(100):='Able was I saw Elba'; --v_word varchar2(100):='saippuakivikauppias'; v_rword varchar2(100); begin select reverse(v_word) into v_rword from dual; dbms_output.put_line(v_word ' - ' v_rword); if (upper(v_word) = upper(v_rword)) then dbms_output.put_line(v_word ' IS A PALINDROME'); else dbms_output.put_line(v_word ' IS NOT A PALIDROME'); end if; end; ---------------------------------------------------------------------------THESE TWO PROCEDURES CAN CHECK,ANY OF THESE SENTENSES IS A PALINDROME --THE WORDS IN THESE SENTENSES ARE SEPARATED WITH MULTIPLE SPACES --'damn i agassi miss again mad' --'ah satan sees natasha' --'A man a plan a canal Panama' -------------------------------------------------------------------------declare --v_word varchar2(1000):='Able was I saw Elba'; --v_word varchar2(1000):=' Damn I Agassi miss mad '; v_word varchar2(1000):='Ah Satan sees Natasha'; v_len number(10); v_cnt number(10):=1; v_alr char(1); v_rlr char(1); v_stat varchar2(100):='IS A PALINDROME'; begin select trim(v_word) into v_word from dual; select length(v_word) into v_len from dual; while (v_len > 0) loop select substr(v_word,v_cnt,1) into v_alr from dual; select substr(v_word,v_len,1) into v_rlr from dual; while ((v_alr IS NULL) OR (v_alr = ' ')) loop v_cnt:= v_cnt + 1; select substr(v_word,v_cnt,1) into v_alr from dual; end loop; while ((v_rlr IS NULL) OR (v_rlr = ' ') AND (v_len > 0)) loop v_len:= v_len - 1; select substr(v_word,v_len,1) into v_rlr from dual; again

end loop; if (upper(v_alr) <> upper(v_rlr)) then v_stat:='IS NOT A PALINDROME'; v_len:=0; else dbms_output.put_line(v_alr ' ' v_rlr); v_len:=v_len-1; v_cnt:=v_cnt+1; end if; end loop; dbms_output.put_line(v_word ' - ' v_stat); end; -------------------------------------------------------------------------set serverout on size 1000000 declare --v_word --v_word v_word ama'; v_word1 v_word2 v_wordr v_ltr v_len varchar2(1000):='Able was I saw Elba'; varchar2(1000):=' Ah Satan sees varchar2(100):='A man a plan a varchar2(1000); varchar2(1000); varchar2(1000); char(1); number;

Natasha '; canal Pan

begin select trim(v_word) into v_word1 from dual; select length(v_word1) into v_len from dual; while (v_len >0) loop select substr(v_word1,v_len,1) into v_ltr from dual; if ((v_ltr IS NOT NULL) AND (v_ltr <> ' ')) then v_word2:=v_word2 v_ltr; end if; v_len:=v_len-1; end loop; select reverse(v_word2) into v_wordr from dual; if (upper(v_word2) = upper(v_wordr)) then dbms_output.put_line(v_word ' - IS A PALINDROME'); dbms_output.put_line(v_word2 ' ' v_wordr); else dbms_output.put_line(v_word ' - IS NOT A PALIDROME'); dbms_output.put_line(v_word2 ' ' v_wordr); end if; end; -------------------------------------------------------------------------References http://en.wiktionary.org/wiki/Appendix:Palindromic_words

----------------------------------------------------------CASE LOGIC 07 DATE 02-14-10 ************* --PROBLEM DEFINITION --WHEN A MATCHING RECORD IS FOUND ON COL1 IN TEMP_JP1, --UPDATE THE RECORD IN TEMP_JP1 WITH DATA FROM TEMP_JP2 TABLE. --WHEN NO MATCHING RECORD ON COL1 IS FOUND --THEN INSERT THE RECORD FROM TEMP_JP2 INTO TEMP_JP1 TABLE. drop table temp_jp1; create table temp_jp1(col1 number,col2 varchar2(20)); drop table temp_jp2; create table temp_jp2(col1 number,col2 varchar2(20)); insert into temp_jp1 values(1,'RAMA'); insert into temp_jp1 values(2,'SITA'); insert into temp_jp2 values(2,'KSIRHNA'); insert into temp_jp2 values(3,'RADHA'); commit; truncate table temp_jp1; truncate table temp_jp2; select * from temp_jp1; COL1 ---------1 2 COL2 -------------------RAMA SITA

select * from temp_jp2; COL1 ---------2 3 COL2 -------------------KSIRHNA RADHA

**SOLUTION 01 **CREATED A PROCEDURE USING A COUNTER V_NUM set serverout on size 100000 declare v_num number(10); begin for c1 in (select col1,col2 from temp_jp2) loop begin execute immediate 'select count(*) from temp_jp1 where col1 = ' c1.col1 into v

_num; if (v_num > 0) then update temp_jp1 set col2 = c1.col2 where col1 = c1.col1; else insert into temp_jp1 values(c1.col1,c1.col2); commit; end if; exception when others then dbms_output.put_line(c1.col1 ' ' c1.col2 ' ' sqlerrm); end; end loop; end; / PL/SQL procedure successfully completed. select * from temp_jp1; COL1 COL2 ---------- -------------------1 RAMA 2 KSIRHNA 3 RADHA ------------------------------------------------------------------------**SOLUTION 02 **CREATED A PROCEDURE USING A SQL%ROWCOUNT CONSTRUCT set serverout on size 1000000 declare begin for c1 in (select col1,col2 from temp_jp2) loop begin execute immediate 'update temp_jp1 set col2 = ''' c1.col2 ''' where col1 = ' c1.col1; if (sql%rowcount = 0) then execute immediate 'insert into temp_jp1 values(' c1.col1 ',''' c1.col2 ''')' ; end if; commit; exception when others then dbms_output.put_line(c1.col1 ' ' c1.col2 ' ' sqlerrm); end; end loop; end; select * from temp_jp1; COL1 COL2 ---------- -------------------1 RAMA 2 KSIRHNA 3 RADHA ------------------------------------------------------------------------**SOLUTION 03 **THE SAME EFFECT IS PRODUCED BY MERGE COMMAND INTRODUCED FROM ORACLE 9I select * from temp_jp1;

COL1 ---------1 2

COL2 -------------------RAMA SITA

select * from temp_jp2; COL1 ---------2 3 COL2 -------------------KSIRHNA RADHA

merge into temp_jp1 a using temp_jp2 b on(a.col1 = b.col1) when matched then update set col2 = b.col2 where col1 = b.col1 when not matched then insert values(b.col1,b.col2); commit; select * from temp_jp1; COL1 COL2 ---------- -------------------1 RAMA 2 KSIRHNA 3 RADHA ------------------------------------------------------------------------**SOLUTION 04 **USING AN FULL OUTER JOIN AND UNION CONSTRUCT WITH DECODE FUNCTION select a.col1,decode(b.col2,null,a.col2,b.col2) col2 from temp_jp1 a, temp_jp2 b where a.col1 = b.col1(+) UNION select b.col1,b.col2 from temp_jp1 a, temp_jp2 b where a.col1(+) = b.col1 / COL1 ---------1 2 3 COL2 -------------------RAMA KSIRHNA RADHA

------------------------------------------------------------------------**SOLUTION 05 --WITHOUT USING A COUNTER AND IF CONDITION --THIS TEST CASE IS FOR ACADEMIC PURPOSE ONLY. --PERFORMANCE WISE NOT AN EFFICIENT OPTION. --USED RETURNING INTO, BUT RETURNING DID NOT WORK IN THE 2ND USAGE. set serverout on size 1000000 declare

v_id temp_jp1.col1%TYPE; begin for c1 in (select col1 from temp_jp2 order by 1 desc) loop begin update temp_jp1 set (col1,col2) = (select col1,col2 from temp_jp2 where col1 = c 1.col1) where col1 = c1.col1 returning c1.col1 into v_id; dbms_output.put_line(v_id ' updated'); insert into temp_jp1 select * from temp_jp2 where col1 = c1.col1 and col1 NOT IN (select col1 from temp_jp1); --returnig c1.col1 into v_id; dbms_output.put_line(v_id ' inserted'); exception when others then dbms_output.put_line(c1.col1 ' ' sqlerrm); end; end loop; dbms_output.put_line(v_id); end; select * from temp_jp1; COL1 COL2 ---------- -------------------1 RAMA 2 KSIRHNA 3 RADHA ----------------------------------------------------------CASE LOGIC 08 DATE 03-14-10 ************* --PROBLEM DEFINITION --FINDING DUPLICATE INDEXES ON TABLES IN ORACLE DATABASE create user jp identified by jp account unlock; alter user jp quota unlimited on users; create table jp.veeksha(empno number,ename varchar2(20)); create index jp.veeksha_idx1 on jp.veeksha(empno); create index jp.veeksha_idx2 on jp.veeksha(empno,ename); set serverout on size 1000000 timing on linesize 200 declare v_name varchar2(20); begin

for c1 in (select table_owner,table_name,column_name,column_position,count(*) fr om dba_ind_columns where table_owner NOT IN ('DBSNMP','EXFSYS','OUTLN','PERFST AT','SYS','SYSMAN','SYSTEM','TOAD','TSMSYS','WMSYS','XDB') and column_position = 1 group by table_owner,table_name,column_name,column_position having count(*) > 1 order by 1,2) loop select name into v_name from v$database; dbms_output.put_line(v_name ' ' c1.table_owner ' ' c1.table_name ' ' c1 .column_name ' '); for c2 in (select index_owner,index_name,column_name,column_position from dba_ind_columns where (index_owner,index_name) in ( select index_owner,index_name from dba_ind_columns where table_owner = c1.table_owner and table_name = c1.table_name and column_name = c1.column_name and column_position = 1) order by index_owner,index_name,column_position) loop dbms_output.put_line(' ' c2.index_owner ' ' c2.index_name ' ' c2.column_ name ' ' c2.column_position); end loop; end loop; end; / PL/SQL procedure successfully completed. TEST JP VEEKSHA EMPNO JP VEEKSHA_IDX1 EMPNO 1 JP VEEKSHA_IDX2 EMPNO 1 JP VEEKSHA_IDX2 ENAME 2 ----------------------------------------------------------CASE LOGIC 09 DATE 04-14-10 ************* --PROBLEM_DEFINITION: --PAPA MADE SOME SWEETS --HER KIDS BABU,MANI,NAGU WENT TO A MOVIE. --BABU WAS HUNGRY, CAME HOME FOUND THE SWEETS, MADE THREE GAVE ONE LEFT OVER SWEET TO THEIR DOG, ATE HIS SHARE OF --MANI WAS HUNGRY, CAME HOME FOUND THE SWEETS, MADE THREE GAVE ONE LEFT OVER SWEET TO THEIR DOG, ATE HER SHARE OF --AFTER THE MOVIE PAPA RETURNED HOME WITH HER THREE KIDS. MADE THE REMAINING SWEETS THREE SHARES, GAVE THE KIDS THEIR SHARE AND GAVE ONE LEFT OVER SWEET TO THEIR DOG. --HOW MANY SWEETS PAPA MADE? --PAPA MADE X NUMBER OF SWEETS --(X-1) IS MADE INTO THREE SHARES, (X-1)/3 EACH --TWO REMAINING SHARES (2X-2)/3 --(2X-2)/3 -1 IS MADE INTO THREE SHARES, (2X-5)/9 EACH --TWO REMAINING SHARES (4X-10)/9 --(4X-10)/9 -1 IS MADE INTO THREE SHARES, (4X-19)/27 EACH --MY PROGRAM CHECK TO SEE WHETHER ANY NUMBER MEETS THE ABOVE REQUIREMENTS:

SHARES, SWEETS. SHARES, SWEETS.

set serverout on size 1000000 timing on declare begin for x in 1..100 loop if mod(x -1,3)=0 then if mod(2*x -5,9)=0 then if mod(4*x -19,27)=0 then dbms_output.put_line('Papa prepared ' x ' sweets'); end if; end if; end if; end loop; end; Papa prepared 25 sweets Papa prepared 52 sweets Papa prepared 79 sweets PL/SQL procedure successfully completed. Elapsed: 00:00:00.01 ----------------------------------------------------------CASE LOGIC 10 DATE 05-14-10 ************* --PROBLEM_DEFINITION: --CASE 01 - YOU ARE ALLOWED TO USE A SIMPLE BALANCE TWICE ONLY --------------------------------------------------------------------------------------------------------------------------------THERE ARE 7 BALLS OF WEIGHT 100 GRAMS EACH AND AN 8TH BALL IS 1 GRAM LESS IN W EIGHT, WEIGHING 99 GRAMS. --YOU ARE ALLOWED TO A BALANCE TWICE ONLY, HOW CAN YOU IDENTIFY THE LESSER WEIGH ING BALL? --TAKE 3 BALLS EACH ON EACH SIDE OF A SIMPLE BALANCE. --FOLLOW THE IF CONDITION OF THE PROCEDURAL ALAGORITHM. set timing on serverout on size 1000000 declare b1 ; b6 --b1 ; b6 number:=100; number:=100; number:=100; number:=100; b2 b7 b2 b7 number:=100; number:=99; number:=100; number:=100; b3 b8 b3 b8 number:=100; b4 number:=100; b5 number:=100 number:=100; number:=99; b4 number:=100; b5 number:=100 number:=100;

t1 number; t2 number; begin select b1 + b2 + b3 into t1 from dual; select b4 + b5 + b6 into t2 from dual; dbms_output.put_line(t1 ' ' t2); if (t1 = t2) then if (b7 > b8) then dbms_output.put_line('b8 is lesser in weight'); else

elsif

dbms_output.put_line('b7 end if; (t1 < t2) then if (b1 = b2) then dbms_output.put_line('b3 elsif (b1 > b2) then dbms_output.put_line('b2 else dbms_output.put_line('b1 end if; if

is lesser in weight');

is lesser in weight'); is lesser in weight'); is lesser in weight');

else (b4 = b5) then dbms_output.put_line('b6 is lesser in weight'); elsif (b4 > b5) then dbms_output.put_line('b5 is lesser in weight'); else dbms_output.put_line('b4 is lesser in weight'); end if;

end if; end; / --PROBLEM_DEFINITION: --CASE 02 - YOU ARE ALLOWED TO USE A SIMPLE BALANCE ONCE ONLY --------------------------------------------------------------------------------------------------------------------------------THERE ARE 8 BOXES CONTAINING 8 BALLS EACH. --7 BOXES ARE CONTAINING 8 BALLS WEIGHING 100 GRAMS EACH. --8TH BOX IS CONTAINING 8 BALLS WEIGHING 99 GRAMS EACH. --USING A BALANCE ONLY ONCE, HOW CAN YOU IDENTIFY THE BOX THAT CONTAINS BALLS WI TH LESSER WEIGHT? --TAKE 1 BALL FROM THE FIRST BOX, 2 BALLS FROM THE SECOND BOX, 3 BALLS FROM THE THIRD BOX,... 8 BALLS FROM THE EIGHTH BOX. --IN TOTAL YOU HAD N(N+1)/2 BALLS. 36 BALLS. set timing on serverout on size 1000000 declare b1 ; b6 --b1 ; b6 number:=100; number:=100; number:=100; number:=100; b2 b7 b2 b7 number:=100; number:=99; number:=100; number:=100; b3 b8 b3 b8 number:=100; b4 number:=100; b5 number:=100 number:=100; number:=99; b4 number:=100; b5 number:=100 number:=100;

t1 number; t2 number; begin select b1 + 2*b2 + 3*b3 + 4*b4 + 5*b5 + 6*b6 + 7*b7 + 8*b8 into t1 from dual; select (3600 - t1) into t2 from dual; dbms_output.put_line('The box containing lesser weighing balls is ' t2); end; / -----------------------------------------------------------

You might also like