P. 1
Logic Works 04

Logic Works 04

|Views: 4|Likes:
Published by jp_vijaykumar
Complex shell and pl/sql scripts
Complex shell and pl/sql scripts

More info:

Published by: jp_vijaykumar on Jul 09, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as TXT, PDF, TXT or read online from Scribd
See more
See less

08/20/2013

pdf

text

original

LOGIC WORKS04 AUTHOR JP VIJAYKUMAR CASE LOGIC 31 ************* --SCRIPT TO DROP ALL OBJECTS IN A SCHEMA.

PLS EXCERSIZE CAUTION --USE THIS SCRIPT AT YOUR OWN RISK AND RESPONSIBILITY. CASE LOGIC 32 ************* --I WANT TO SCP LARGE EXPORT DUMP FILES ACROSS THE NETWORK BETWEEN SERVERS. --FOR SOME REASON, OUR SCP CAN NOT SPAWN MORE THAN 10 CONCURRENT SESSION. AFTER 10 CONCURRENT SESSIONS, THE JOB FAILS. THIS IS THE WORK AROUND. CASE LOGIC 33 ************* --THIS SHELL SCRIPT LOOPS THROUGH ALL THE NODES IN THE CLUSTER AND DOES SOME HEALTH CHECK AT THE OS LEVEL FOR PERFORMANCE REVIEW. CASE LOGIC 34 ************* --THIS ONE LINE COMMAND LOOPS THROUGH ALL THE SIDS RUNNING ON A NODE AND EXECUTE S SQL SCRIPTS TO CHECK THE HEALTH OF EACH ORACLE DB RUNNING ON A SPECIFIC NODE. CASE LOGIC 35 ************* --COMBINE CASE LOGIC 23 AND 24 INTO ONE SCRIPT TO LOOP THROUGH EACH NODE AND EAC H DB RUNNING ON EACH NODE. CASE LOGIC 36 ************* --THIS SCRIPT FIND THE YEAR WITH < 365 DAYS. SEPTEMBER MONTH IN 1752 YEAR HAD ONLY 19 DAYS (11 DAYS LESS THAN A NORMAL SEPTEMBER MONTH) CASE LOGIC 37 ************* --TO FIND THE ROW COUNTS FROM THE LAST TWO PARTITIONS OF ALL PARTITIONED TABLES IN A SCHEMA. HERE I AM USING THE MAX PARTITION COUNT, TO FIND # ROWS CASE LOGIC 38 ************* --TO FIND THE ROW COUNTS FROM THE LAST TWO PARTITIONS OF ALL PARTITIONED TABLES IN A SCHEMA. HERE I AM USING THE PARTITION_POSITION WITH ORDER BY CLAUSE TO FIND NUM_ROWS CASE LOGIC 39 ************* --HERE IS YOUR HOMEWORK, COMPARE AND CONTRAST SCRIPTS FROM CASE LOGIC 27 AND 28 AND FIND OUT THE EFFICIENT SCRIPT WHICH AND WHY. CASE LOGIC 40 ************* --THE SIZES OF THE DBS ARE GROWING EXPONENTIALLY. TO CUT THE RUN TIMES ON TABLES' STATISTICS GATHERING, I GENERATED THIS SCRIPT TO

CAPTURE THE LAST PARTITION OF A RANGE PARTITIONED TABLE WITH DATE RANGE PARTIT ION KEY, AND ALL PARTITIONS FOR OTHER TYPES OF PARTITIONS AND NON PARTITIONED TABL ES. -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* -*-* ***************SOLUTION DETAILS************************************************* **** -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* -*-* CASE LOGIC 31 DATE 05-14-11 ************* --THIS IS A DANGEROUS SCRIPT. PLS EXCERSIZE CAUTION --USE THIS SCRIPT AT YOUR OWN RISK AND RESPONSIBILITY set serverout on size 1000000 timing on declare v_ltr char(1); v_num number; begin execute immediate 'select substr(name,length(name),1) from v$database' into v_lt r; --OUR PRODUCTION DB NAMES END WITH A "P". THIS SCRIPT WILL NOT WORK IN PRODUCTIO N DBS. if (v_ltr <> 'P') then <<BEGIN_LOOP>> for c1 in (select object_name,object_type from user_objects order by object_type desc) loop begin if (c1.object_type = 'TABLE') then for c2 in (select a.table_name,a.constraint_name from user_constraints a, user_constraints b where a.constraint_type = 'R' and a.r_constraint_name = b.constraint_name and b.table_name = c1.object_name) loop begin execute immediate 'alter table ' c2.table_name ' drop constraint ' c2.constra int_name; exception when others then dbms_output.put_line(c2.table_name ' ' c2.constraint_name ' ' sqlerrm); end; end loop; end if; --execute immediate 'drop ' c1.object_type ' ' c1.object_name; --UNCOMM ENT THIS LINE BEFORE EXECUTING THE SCRIPT dbms_output.put_line('drop ' c1.object_type ' ' c1.object_name); --COMMEN T THIS LINE BEFORE EXECUTING THE SCRIPT exception when others then dbms_output.put_line(c1.object_name ' ' sqlerrm); end; end loop; execute immediate 'select count(1) from user_objects' into v_num; if (v_num > 0) then

dbms_output.put_line('Num table: ' v_num); goto BEGIN_LOOP; else dbms_output.put_line('All objects were dropped'); end if; end if; end; / -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* -*-* ----------------------------------------------------------------------------------CASE LOGIC 32 DATE 05-14-11 ************* --I WANT TO SCP LARGE EXPORT DUMP FILES ACROSS THE NETWORK BETWEEN SERVERS. --FOR SOME REASON, OUR SCP CAN NOT SPAWN MORE THAN 10 CONCURRENT SESSION. AFTER 10 CONCURRENT SESSIONS, THE JOB FAILS. THIS IS THE WORK AROUND. oracle1@linux019:~/scripts/jp> cat parallel_scp.sh #!/bin/ksh echo "scp started `date`" export SOURCE_DIR=/apps/oracle2/export/PROD138P/save4qtr #Here provide the file name without the last number and .dmp extention to be scp ed export FILE_NAME=PROD138P_PRODDB_20000421_215959_27168 export DEST_HOST=linux020 export DEST_DIR=/apps/oracle2/export/PROD018D/save4qtr export FILE_NUM=0 cd $SOURCE_DIR ls -1tr $FILE_NAME* while read FNAME do echo $FNAME $DEST $DEST_DIR scp $FNAME oracle1@$DEST:$DEST_DIR & echo "scp $FNAME oracle1@$DEST:$DEST_DIR &" FILE_NUM=`expr $FILE_NUM + 1` if [[ $FILE_NUM -eq "8" ]]; then sleep 20; FILE_NUM=0; fi done wait echo "scp completed `date`" exit -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* -*-* CASE LOGIC 33 DATE 05-14-11 ************* --THIS SQL SCRIPT SWITCHES BETWEEN USERS INTERACTIVELY AND PROMPTS FOR THE SCHEMA PASSWORD AND EXECUTES SQL COMMANDS AS THE SCHEMA OWNER.

prompt enter password for scott user in devdb db connect scott@devdb show user select count(1) from user_tables; prompt enter password for tom user in devdb db connect tom@devdb show user select count(1) from user_tables; prompt enter password for harry user in devdb db connect harry@devdb show user select count(1) from user_tables; exit -------------------------------------------------------C:\Users\jp\scripts>sqlplus scott@devdb SQL*Plus: Release 10.2.0.3.0 - Production on Thu Apr 21 12:29:15 2011 Copyright (c) 1982, 2006, Oracle. All Rights Reserved. Enter password: Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production With the Partitioning, Real Application Clusters, OLAP, Data Mining and Real Application Testing options SQL> @test.txt enter password for scott user in devdb db Enter password: Connected. USER is "scott" COUNT(1) ---------117 enter password for tom user in devdb db Enter password: Connected. USER is "tom" COUNT(1) ---------73 enter password for harry user in devdb db Enter password: Connected. USER is "harry" COUNT(1) ---------152 Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64

bit Production With the Partitioning, Real Application Clusters, OLAP, Data Mining and Real Application Testing options C:\Users\jp\scripts> -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* -*-* CASE LOGIC 34 DATE 05-14-11 ************* --I WANT TO FIND ALL THE TABLES, WITH EACH TABLE'S SIZE + SIZE OF ALL INDEXES CREATED ON THE TABLE + SIZE OF ALL LOB SEGMENTS CREATED IN THE TABLE. set serverout on size 1000000 timing on linesize 200 declare v_size number; begin dbms_output.put_line('TABLE_NAME,NUM_ROWS,SEGMENT_NAME,SEGMENT_TYPE,SIZE_MB'); for c1 in (select owner,table_name,num_rows from dba_tables where owner='SCOTT' order by 1) loop for c2 in ( select sum(size_mg) sum_size_mg from ( select segment_name,segment_type,round(sum(bytes)/1024/1024) size_mg from dba_segments where segment_name in (select segment_name from dba_segments where owner = c1.owner and segment_name = c1.table_name union select index_name from dba_indexes where table_owner = c1.owner and table_name = c1.table_name union select segment_name from dba_lobs where owner = c1.owner and table_name = c1.table_name) group by segment_name,segment_type order by 1) )loop dbms_output.put_line(c1.table_name ' ,' c1.num_rows ',' c2.sum_size_mg); end loop; end loop; end; / -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* -*-* CASE LOGIC 35 DATE 05-14-11 ************* --I WANT TO FIND ALL THE TABLES IN A SCHEMA WITH THE SIZE OF EACH TABLE, SUM SIZ E OF ALL INDEXES CREATED ON THE TABLE, SUM SIZE OF ALL LOB SEGMENTS CREATED IN T HE TABLE DISPLAYED IN A LINE. --THIS SCRIPT IS A RESOURCE INTENSIVE SCRIPT AND TAKES A LONG TIME TO COMPLETE.

set serverout on size 10000 timing on declare v_tab_size_mb number; v_ind_size_mb number; v_lob_size_mb number; begin for c1 in (select owner,table_name,num_rows from dba_tables where owner = 'SCOTT' order by table_name) loop execute immediate 'select round(sum(bytes)/1024/1024,2) from dba_segments where owner =''' c1.owner ''' and segment_name = ''' c1.table_name ''' ' into v_tab_si ze_mb; execute immediate 'select nvl(round(sum(bytes)/1024/1024,2),0) from dba_segment s where (owner ,segment_name) in ( select owner,index_name from dba_indexes where owner = ''' c1.owner ''' and table_name = ''' c1.table_name ''' )' into v_ind_size_mb; execute immediate 'select nvl(round(sum(bytes)/1024/1024,2),0) from dba_segment s where (owner ,segment_name) in ( select owner,segment_name from dba_lobs where owner = ''' c1.owner ''' and table_name = ''' c1.table_name ''') ' into v_lob_size_mb; dbms_output.put_line(c1.owner ' ' c1.table_name ' ' c1.num_rows ' ' v_tab_ size_mb ' ' v_ind_size_mb ' ' v_lob_size_mb); end loop; end; / SCOTT SCOTT SCOTT SCOTT BONUS 0 .06 0 0 DEPT 4 .06 .06 0 EMP 14 .06 .06 0 SALGRADE 15 .06 0 0

PL/SQL procedure successfully completed. --PERFORMANCE WISE THIS SCRIPT IS AN EFFICIENT AND RUNS IN LESS TIME. set serverout on size 10000 timing on declare v_tab_size_mb number; v_ind_size_mb number; v_lob_size_mb number; begin for c1 in (select owner,table_name,num_rows from dba_tables where owner='DEMO' order by table_name) loop execute immediate 'select sum(bytes)/1024/1024 from dba_segments where owner = : b1 and segment_name = :b2 ' into v_tab_size_mb using c1.owner,c1.table_name; execute immediate 'select nvl(round(sum(bytes)/1024/1024),0) from dba_segments where (owner ,segment_name) in ( select owner,index_name from dba_indexes where owner = :b1 and table_name = :b2)' into v_ind_size_mb using c1.owne r,c1.table_name; execute immediate 'select nvl(round(sum(bytes)/1024/1024),0) from dba_segments where (owner ,segment_name) in ( select owner,segment_name from dba_lobs

where owner = :b1 and table_name = :b2)' into v_lob_size_mb using c1.owne r,c1.table_name; dbms_output.put_line(c1.owner ' ' c1.table_name ' ' c1.num_rows ' ' v_tab_size_mb ' ' v_ind_size_mb ' ' v_lob_size_mb); end loop; end; / -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* -*-* CASE LOGIC 36 DATE 05-14-11 ************* --AT REGULAR INTERVALS, I HAVE TO TRUNCATE SOME TABLES FROM A SCHEMA. MOST OF THE TIME, THESE TABLES ARE LOCKED BY USERS. FIRST I NEED TO KILL THE SESSIONS, THAT ARE LOCKING THE TABLE AND TRUNCATE THE TABLES. --THIS SCRIPT DOES THE WORK. set echo on feedback on timing on serverout on size 1000000 declare v_tab varchar2(30); begin for c1 in (select object_id, object_name from dba_objects where owner = 'SCOTT' and object_type='TABLE' order by object_name) loop for c2 in (select l.session_id,s.serial# from v$locked_object l, v$session s where l.object_id = c1.object_id and l.session_id = s.sid ) loop dbms_output.put_line('alter system kill session ' c2.session_id ',' c2.se rial# ' immediate;'); end loop; dbms_output.put_line('truncate table ' end loop; end; / --THIS SCRIPT WORKS FINE, IF YOU ARE THE DBA. References: http://www.orafaq.com/node/2329 -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* -*-* CASE LOGIC 37 DATE 05-14-11 ************* --SAME SCENARIO, AS IN CASE LOGIC 36, I DO NOT HAVE DBA ROLE. --THIS IS THE WORK AROUND SCRIPT, TO KILL ALL LOCKING SESSIONS AND TRUNCATE TABL ES. create table temp_jp(col1 number); c1.object_name ';');

insert into temp_jp values(1); --FROM A 2ND SESSION. DO NOT COMMIT THE INSERT COMMAND. SQL> drop table temp_jp; drop table temp_jp * ERROR at line 1: ORA-00054: resource busy and acquire with NOWAIT specified select --inst_id, process,'' chr(10) '!ssh ' (select host_name from gv$instance where inst_id = a.inst_id) ' ki ll -9 ' process from gv$session a where sid in ( select session_id from gv$locked_object where object_id in ( select object_id from dba_objects where object_name='TEMP_JP')); select '!ssh ' (select host_name from gv$instance where inst_id = s.inst_id) ' ki ll -9 ' s.process, '' chr(10) ' drop table ' o.object_name ';' from gv$session s, gv$locked_object l, user_objects o where s.sid = l.session_id and l.object_id = o.object_id; set echo off feedback off pagesize 0 spool jp.dat / spool off set echo on feedback on pagesize 20 SQL> !cat jp.dat sed '/SQL/d' > TEMP.sql

SQL> !cat TEMP.sql !ssh linuxora20 kill -9 12023 drop table TEMP_JP; INST ID PROCESS ---- -----------'' CHR(10) '!SSH' (SELECTHOST_NAMEFROMGV$INSTANCEWHEREINST_ID=A.INST_ID) 'KI -------------------------------------------------------------------------------1 25011 !ssh linuxora20 kill -9 25011 drop table temp_jp;

set echo off feedback off timing off serverout on size 1000000 timing off declare v_tab varchar2(30); begin for c1 in (select distinct object_id object_id from gv$locked_object

where object_id in (select object_id from user_objects) order b y 1) loop for c2 in (select '!ssh ' (select host_name from gv$instance where inst_id = l.inst_id) ' kill -9 ' process cmd from gv$locked_object l where object_id = c1.object_id) loop dbms_output.put_line(c2.cmd); dbms_output.put_line('!sleep 1'); end loop; execute immediate 'select object_name from user_objects where object_id = :b 1' into v_tab using c1.object_id; dbms_output.put_line('drop table ' v_tab ';'); end loop; end; / set echo on feedback onSQL> !ssh linuxora20 kill -9 690 !sleep 1 !ssh linuxora20 kill -9 1337 !sleep 1 drop table TEMP_JP; !ssh linuxora20 kill -9 2725 !sleep 1 !ssh linuxora26 kill -9 31885 !sleep 1 drop table TEMP_JP1; SQL> SQL> SQL> SQL> SQL> !cat jp.dat sed '/SQL/d' > TEMP.sql SQL> !cat TEMP.sql !ssh linuxora20 kill !sleep 1 !ssh linuxora20 kill !sleep 1 drop table TEMP_JP; !ssh linuxora20 kill !sleep 1 !ssh linuxora26 kill !sleep 1 drop table TEMP_JP1; -9 690 -9 1337 -9 2725 -9 31885

SQL> @TEMP.sql SQL> !ssh linuxora20 kill -9 690 SQL> !sleep 1 SQL> !ssh linuxora20 kill -9 1337 SQL> !sleep 1 SQL> drop table TEMP_JP; Table dropped. SQL> !ssh linuxora20 kill -9 2725 SQL> !sleep 1 SQL> !ssh linuxora26 kill -9 31885

SQL> !sleep 1 SQL> drop table TEMP_JP1; Table dropped. References: http://download.oracle.com/docs/cd/B19306_01/server.102/b14237/dynviews_1149.htm http://www.toadworld.com/BLOGS/tabid/67/EntryId/228/How-to-Run-an-OS-Command-fro m-PL-SQL.aspx -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* -*-* CASE LOGIC 38 DATE 05-14-11 ************* --TO FIND THE MODE OF CODE COLUMN FROM A TABLE. IF THE MODE OF CODE IN A TABLE IS "0" THEN TAKE THE NEXT AVAILABLE HIGHER MODE VALUE. IF TWO CODES IN A TABLE EXIST EQUAL NUMBER OF TIMES, THEN TAKE THE HIGHER VALU E. --THE BUSINESS MAY TO CHOOSE THE LOWER VALUE,IF TWO CODES EXIST EQUAL NUMBER OF TIMES. THE CODE CAN BE MODIFIED TO TAKE THE LOWER VALUE OF CODE IN A TABLE AS THE MOD E. --USING IF CONDITION AND NESTED CURSORS: drop drop drop drop drop table table table table table temp_jp1; temp_jp2; temp_jp3; temp_jp4; temp_jp5; varchar2(20),barcode char(5),dept char(2),code numbe varchar2(20),barcode char(5),dept char(2),code numbe varchar2(20),barcode char(5),dept char(2),code numbe varchar2(20),barcode char(5),dept char(2),code numbe varchar2(20),barcode char(5),dept char(2),code numbe

create table temp_jp1(ename r) tablespace users; create table temp_jp2(ename r) tablespace users; create table temp_jp3(ename r) tablespace users; create table temp_jp4(ename r) tablespace users; create table temp_jp5(ename r) tablespace users; insert insert insert insert insert insert insert insert insert insert insert into into into into into into into into into into into temp_jp1 temp_jp1 temp_jp1 temp_jp1 temp_jp1 temp_jp1 temp_jp2 temp_jp2 temp_jp2 temp_jp2 temp_jp2

values('JP','06-17',6,15) ; values('LAKSHMI','06-17',10,5); values('VINAY','00-00',9,14); values('VENKATA','09-22',8,12); values('VINEELA','09-22',1,4); values('VEEKSHA','00-00',9,14); values('SUBBARAO',null,null,0); values('PAPADEVI',null,null,0); values('BABU','05-31',7,7); values('DURGA','05-31',7,7); values('KIRAN','00-00',9,22);

insert insert insert insert insert insert insert insert insert insert insert 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 into into into into into into into into into into into

temp_jp2 temp_jp2 temp_jp2 temp_jp2 temp_jp2 temp_jp2 temp_jp2 temp_jp2 temp_jp2 temp_jp3 temp_jp3 temp_jp3 temp_jp3 temp_jp4 temp_jp4 temp_jp4 temp_jp4 temp_jp5 temp_jp5 temp_jp5 temp_jp5 temp_jp5 temp_jp5 temp_jp5 temp_jp5 temp_jp5 temp_jp5 temp_jp5

values('ADITHYA','00-00',9,5); values('NAGAMANI','09-15',4,18); values('SHAMBHU','09-15',1,20); values('BHARADWAJ','00-00',1,27); values('SUBHASH','00-00',11,3); values('NAGESWARI','02-24',9,19); values('SRINIVAS','02-24',9,19); values('MAHESH','00-00',1,29); values('MANASA','00-00',4,12); values('SUNDARAM',null,null,0); values('SAVITRI',null,null,0); values('VIJAYA',null,1,1); values('BALU',null,null,0); values('RAMAMOHAN','05-11',5,14); values('MAHALAKSHMI','05-11',4,26); values('MALIKA','08-28',12,7); values('KARTHIK','05-18',12,7); values('SURYANARAYANA',null,1,1); values('SUBHADRA',null,null,0); values('RAMA',null,9,8); values('SRINIVAS',null,9,8); values('KARTHIK',null,9,8); values('DIVYA',null,9,8); values('NAGU',null,9,8); values('SIVA',null,9,8); values('SHREYA',null,9,8); values('MARUTHI',null,9,8); values('GAYATRY',null,9,8);

SQL> set pagesize 100 SQL> select * from temp_jp1; ENAME -------------------JP LAKSHMI VINAY VENKATA VINEELA VEEKSHA 6 rows selected. SQL> select * from temp_jp2; ENAME -------------------SUBBARAO PAPADEVI BABU DURGA KIRAN ADITHYA NAGAMANI SHAMBHU BHARADWAJ SUBHASH BARCO DE CODE ----- -- ---------0 0 05-31 7 7 05-31 7 7 00-00 9 22 00-00 9 5 09-15 4 18 09-15 1 20 00-00 1 27 00-00 11 3 BARCO ----06-17 06-17 00-00 09-22 09-22 00-00 DE CODE -- ---------6 15 10 5 9 14 8 12 1 4 9 14

NAGESWARI SRINIVAS MAHESH MANASA 14 rows selected.

02-24 02-24 00-00 00-00

9 9 1 4

19 19 29 12

SQL> select * from temp_jp3; ENAME BARCO DE CODE -------------------- ----- -- ---------SUNDARAM 0 SAVITRI 0 VIJAYA 1 1 BALU 0 SQL> select * from temp_jp4; ENAME -------------------RAMAMOHAN MAHALAKSHMI MALIKA KARTHIK BARCO ----05-11 05-11 08-28 05-18 DE CODE -- ---------5 14 4 26 12 7 12 7

SQL> select * from temp_jp5; ENAME BARCO DE CODE -------------------- ----- -- ---------SURYANARAYANA 1 1 SUBHADRA 0 RAMA 9 8 SRINIVAS 9 8 KARTHIK 9 8 DIVYA 9 8 NAGU 9 8 SIVA 9 8 SHREYA 9 8 MARUTHI 9 8 GAYATRY 9 8 11 rows selected. SQL> select code,count(*) from temp_jp1 group by code order by 2 desc,1 desc; CODE COUNT(*) ---------- ---------14 2 15 1 12 1 5 1 4 1 SQL> select code,count(*) from temp_jp2 group by code order by 2 desc,1 desc; CODE COUNT(*) ---------- ---------19 2 7 2 0 2

29 27 22 20 18 12 5 3 11 rows selected.

1 1 1 1 1 1 1 1

SQL> select code,count(*) from temp_jp3 group by code order by 2 desc,1 desc; CODE COUNT(*) ---------- ---------0 3 1 1 SQL> select code,count(*) from temp_jp4 group by code order by 2 desc,1 desc; CODE COUNT(*) ---------- ---------7 2 26 1 14 1 SQL> select code,count(*) from temp_jp5 group by code order by 2 desc,1 desc; CODE COUNT(*) ---------- ---------8 9 1 1 0 1 SQL> select stats_mode(code) from temp_jp1; STATS_MODE(CODE) ---------------14 SQL> select stats_mode(code) from temp_jp2; STATS_MODE(CODE) ---------------0 SQL> select stats_mode(code) from temp_jp3; STATS_MODE(CODE) ---------------0 SQL> select stats_mode(code) from temp_jp4; STATS_MODE(CODE) ---------------7 SQL> select stats_mode(code) from temp_jp5;

STATS_MODE(CODE) ---------------8

drop view temp_view; create or replace view temp_view as select 1 ename,2 code from dual; set serverout on size 1000000 timing on declare v_mode number; v_rows number; begin for c1 in (select table_name from user_tables where table_name like 'TEMP_JP%' order by 1) loop execute immediate 'create or replace view TEMP_VIEW as select ename,code from ' c1.table_name; dbms_output.put_line(c1.table_name); v_mode:=0; v_rows:=0; for c2 in (select code,count(1) num_rows from temp_view group by code order by num_rows desc,code desc) loop if (( v_mode = 0 ) and ( v_rows = 0 )) then v_mode:=c2.code; v_rows:=c2.num_rows; elsif (( v_mode = 0 ) and ( v_rows <> 0 )) then v_mode:=c2.code; v_rows:=c2.num_rows; end if; end loop; dbms_output.put_line(v_mode ' ' v_rows); end loop; end; / TEMP_JP1 14 2 TEMP_JP2 19 2 TEMP_JP3 1 1 TEMP_JP4 7 2 TEMP_JP5 8 9 PL/SQL procedure successfully completed. Elapsed: 00:00:00.34 Reference: http://www.purplemath.com/modules/meanmode.htm http://www.mathgoodies.com/lessons/vol8/mode.html http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:173297303620 10

http://www.dbasupport.com/oracle/ora10g/multi_table_loop.shtml -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* -*-* CASE LOGIC 39 DATE 04-14-11 ************* --HANDLING THE SAME ISSUE FROM CASE LOGIC 38 IN A DIFFERENT WAY. --TO FIND THE MODE OF CODE COLUMN FROM A TABLE. IF THE MODE OF CODE IN A TABLE IS "0" THEN TAKE THE NEXT AVAILABLE HIGHER MODE VALUE. IF TWO CODES IN A TABLE EXIST EQUAL NUMBER OF TIMES, THEN TAKE THE HIGHER VALU E. --THE BUSINESS MAY TO CHOOSE THE LOWER VALUE,IF TWO CODES EXIST EQUAL NUMBER OF TIMES. THE CODE CAN BE MODIFIED TO TAKE THE LOWER VALUE OF CODE IN A TABLE AS THE MOD E. --USING ROWNUM AND CASE FUNCTION: drop view temp_view; create or replace view temp_view as select 1 ename,2 code from dual; set serverout on size 1000000 timing on declare v_mode number; v_rows number; begin for c1 in (select table_name from user_tables where table_name like 'TEMP_JP%' order by 1) loop execute immediate 'create or replace view TEMP_VIEW as select ename,code from ' c1.table_name; dbms_output.put_line(c1.table_name); for c2 in (select case when ( select code from ( select rownum row_num,t.* from ( select code,count(1) num_rows from temp_view group by code order by num_rows desc) t) where row_num=1) = 0 and (select count(1) from ( select code,count(1) num_rows from temp_view group by code order by num_rows desc)) > 1 then ( select code from ( select rownum row_num,t.* from ( select code,count(1) num_rows from temp_view group by code order by num_rows desc) t) where row_num=2) else ( select code from ( select rownum row_num,t.* from ( select code,count(1) num_rows from temp_view group by code order by num_rows desc) t) where row_num=1) end code from dual ) loop

select num_rows into v_rows from ( select code,count(1) num_rows from temp_view group by code order by num_rows desc,code desc) where code = c2.code; dbms_output.put_line(c2.code ' ' v_rows); end loop; end loop; end; / TEMP_JP1 14 2 TEMP_JP2 19 2 TEMP_JP3 1 1 TEMP_JP4 7 2 TEMP_JP5 8 9 PL/SQL procedure successfully completed. Elapsed: 00:00:00.38 select case when code from (select rownum row_num,t.* from (select code,count(1) num_rows from temp_view group by code order by num_rows desc) t) where row_num=1;

select case when (select code from (select rownum row_num,t.* from (select code,count(1) num_rows from temp_view group by code order by num _rows desc) t) where row_num=1) = 0 and (select count(1) from (select code,count(1) num_rows from temp_view group by code order by num _rows desc)) > 1 then (select code,num_rows from (select rownum row_num,t.* from (sele ct code,count(1) num_rows from temp_view group by code order by num _rows desc) t) where row_num=2) else (select code,num_rows from (select rownum row_num,t.* from (sele ct code,count(1) num_rows from temp_view group by code order by num _rows desc) t) where row_num=1) end from dual; Reference: http://www.purplemath.com/modules/meanmode.htm http://www.mathgoodies.com/lessons/vol8/mode.html http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:173297303620 10 http://www.dbasupport.com/oracle/ora10g/multi_table_loop.shtml -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* -*-* CASE LOGIC 40

DATE 04-14-11 ************* --HANDLING THE SAME ISSUE FROM CASE LOGIC 38 IN A DIFFERENT WAY. --TO FIND THE MODE OF CODE COLUMN FROM A TABLE. IF THE MODE OF CODE IN A TABLE IS "0" THEN TAKE THE NEXT AVAILABLE HIGHER MODE VALUE. IF TWO CODES IN A TABLE EXIST EQUAL NUMBER OF TIMES, THEN TAKE THE HIGHER VALU E. --THE BUSINESS MAY TO CHOOSE THE LOWER VALUE,IF TWO CODES EXIST EQUAL NUMBER OF TIMES. THE CODE CAN BE MODIFIED TO TAKE THE LOWER VALUE OF CODE IN A TABLE AS THE MOD E. --CREATING A VIEW AND USING ROWNUM: drop view temp_view; create or replace view temp_view as select 1 ename,2 code from dual; set serverout on size 1000000 timing on declare v_mode number; v_rows number; begin for c1 in (select table_name from user_tables where table_name like 'TEMP_JP%' order by 1) loop execute immediate 'create or replace view TEMP_VIEW as select code, count(1) num _rows from ' c1.table_name ' group by code order by num_rows desc, code desc'; dbms_output.put_line(c1.table_name); execute immediate 'select code,num_rows from temp_view where code <> 0 and rownu m =1' into v_mode,v_rows; dbms_output.put_line(v_mode ' ' v_rows); end loop; end; / TEMP_JP1 14 2 TEMP_JP2 19 2 TEMP_JP3 1 1 TEMP_JP4 7 2 TEMP_JP5 8 9 PL/SQL procedure successfully completed. Elapsed: 00:00:00.38

Reference:

http://www.purplemath.com/modules/meanmode.htm http://www.mathgoodies.com/lessons/vol8/mode.html http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:173297303620 10 http://www.dbasupport.com/oracle/ora10g/multi_table_loop.shtml -----------------------------------------------------------------------------------------

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->