公司為期了兩周的oracle培訓,今天進行了一次測試,現將其中三道程式題供大家分享,共同學習討論。
1、設計乙個函式,給出物件號,返回物件名
create or replace function get_obj_name (v_id number)
return varchar2
isv_name varchar2(100);
cursor c(o_id number) is
select object_name from dba_objects
where object_id=o_id;
begin
open c(v_id);
fetch c into v_name;
if c%notfound then
v_name:='not found';
end if;
close c;
return v_name;
end;
/select get_obj_name(124) from dual;
select get_obj_name(124334343) from dual;
2、設計乙個程式,顯示某錶空間中空間使用情況。dba_free_space
tablespace_name:表空間名
file_id :空閒空間所在的檔案號
block_id:空閒空間開始的塊號
blocks : 空閒空間大小
create or replace procedure list_free_space
(v_name varchar2, ncol number,used varchar2,freed varchar2)
is cursor c(t_name varchar2) is
select * from dba_free_space
where tablespace_name=t_name
order by file_id,block_id;
cursor c2(t1_name varchar2) is
select blocks from dba_data_files
where tablespace_name=t1_name;
j number:=1;
k number:=1;
m number:=1;
begin
for x in c(v_name) loop
for i in j..x.block_id-1 loop
dbms_output.put(used);
k:=k+1;
if k>=ncol then
dbms_output.put_line('|');
k:=1;
end if;
end loop;
for i in 1..x.blocks loop
dbms_output.put(freed);
k:=k+1;
if k>=ncol then
dbms_output.put_line('|');
k:=1;
end if;
end loop;
j:=x.block_id+x.blocks;
end loop;
open c2(v_name);
fetch c2 into m;
m:=1;
for i in 1..m-j loop
dbms_output.put(used);
k:=k+1;
if k>=ncol then
dbms_output.put_line('|');
k:=1;
end if;
end loop;
close c2;
close c;
dbms_output.put_line('end');
null;
end;
/select blocks from dba_data_files where tablespace_name='undotbs1';
set serveroutput on size 50000
set linesize 100
exec list_free_space('undotbs1',30,'*','_');
3、需要經常統計每個會話的邏輯讀和物理讀資訊,直接使用sql很不方便,設計乙個程式,
簡化檢視某個會話邏輯讀、物理讀的過程
create or replace package my_stat is
type stat is record(
value number,
name varchar2(100));
type var_stat is table of stat index by binary_integer;
n_stat var_stat;
n_stat_tmp number;
procedure get_stat(v_sid number,v_name varchar2);
end my_stat;
/create or replace package body my_stat is
procedure get_stat(v_sid number,v_name varchar2) is
cursor c1(c_sid number,c_name varchar2)
is select a.name,b.value
from v$statname a,v$sesstat b where
a.statistic#=b.statistic# and
a.name=c_name and b.sid=c_sid;
i number;
begin
if n_stat_tmp = 0 then
n_stat_tmp:=1;
n_stat(n_stat_tmp).value:=0;
n_stat(n_stat_tmp).name:=v_name;
else
i:=1;
while i<=n_stat.count loop
if n_stat(i).name = v_name then
n_stat_tmp:=i;
goto exit_loop;
end if;
i:=i+1;
end loop;
if i=n_stat.count+1 then
n_stat(i).value:=0;
n_stat(i).name:=v_name;
n_stat_tmp:=i;
end if;
end if;
<>
for x in c1(v_sid,v_name) loop
dbms_output.put_line(x.value-n_stat(n_stat_tmp).value );
n_stat(n_stat_tmp).value:=x.value;
end loop;
end get_stat;
begin
n_stat_tmp:=0;
end my_stat;
幾道趣味題
1.有乙個隨機數發生器,可以產生1到5的隨機數,利用這個隨機發生器,怎樣產生1到7的隨機數 最直接的想法是拿隨機數乘以7然後除以5,但這樣產生的結果並不是等概率的,7 rand 5中產生不了3和6,因而不可行 正確的做法是5 rand rand 然後捨棄最後4個數,剩餘21個數字。每三個數分為一組,...
幾道演算法題
1 n階乘之和 public class test1 sum kk system.out.println sum 2.獲取二維陣列每列最小的值 public class test2 system.out.println sum public long jiecheng int x return su...
前端筆試的幾道題
js常用內建物件 js中內建了17個物件,常用的是array物件 date物件 正規表示式物件 string物件 global物件 可訪問函式引數的內建變數 arguments類陣列 sprite最關鍵屬性 background position dom操作中用於元素導航欄的屬性 parentnod...