经典sql语句Word文档格式.docx
《经典sql语句Word文档格式.docx》由会员分享,可在线阅读,更多相关《经典sql语句Word文档格式.docx(25页珍藏版)》请在冰豆网上搜索。
begin
forvin1..5loop
forv_countin1..3loop
v_prior_rand:
=floor(dbms_random.value(1,priornum));
v_end_rand:
=v_prior_rand;
<
random>
>
ifv_prior_rand!
=v_end_randthen
v_string:
=v_string||'
'
||v_prior_rand||'
||v_end_rand;
else
gotorandom;
endif;
dbms_output.put_line(v_string);
endloop;
蓝色球,'
||floor(dbms_random.value(1,endnum))||'
红色球'
returnv_string;
endf_ssqrandom;
查询新建用户
selectusername
fromdba_users
whereusernamenotin
('
TEXT'
'
RMAN_USER'
TEST'
SCOTT'
TSMSYS'
MDDATA'
DIP'
'
DBSNMP'
SYSMAN'
MDSYS'
ORDSYS'
EXFSYS'
DMSYS'
WMSYS'
CTXSYS'
ANONYMOUS'
XDB'
ORDPLUGINS'
SI_INFORMTN_SCHEMA'
OLAPSYS'
MGMT_VIEW'
SYS'
SYSTEM'
OUTLN'
);
查询那些用户,操纵了那些表造成了锁机
SELECTs.username,
decode(l.type,'
TM'
TABLELOCK'
'
TX'
ROWLOCK'
NULL)LOCK_LEVEL,
o.owner,o.object_name,o.object_type,
s.sid,s.serial#,s.terminal,s.machine,s.program,s.osuser
FROMv$sessions,v$lockl,all_objectso
WHEREl.sid=s.sid
ANDl.id1=o.object_id(+)
ANDs.usernameisNOTNull
其中TM为表锁定TX为行锁定
看锁阻塞的方法是
SELECT(selectusernameFROMv$sessionWHEREsid=a.sid)blocker,
a.sid,
isblocking'
(selectusernameFROMv$sessionWHEREsid=b.sid)blockee,
b.sid
FROMv$locka,v$lockb
WHEREa.block=1
ANDb.request>
0
ANDa.id1=b.id1
ANDa.id2=b.id2--各进制之间转换
CREATEORREPLACETYPEtype_str_aggASOBJECT
(
totalVARCHAR2(4000),
STATICFUNCTIONodciaggregateinitialize(sctxINOUTtype_str_agg)
RETURNNUMBER,
MEMBERFUNCTIONodciaggregateiterate
(
SELFINOUTtype_str_agg,
VALUEINVARCHAR2
)RETURNNUMBER,
MEMBERFUNCTIONodciaggregateterminate
SELFINtype_str_agg,
returnvalueOUTVARCHAR2,
flagsINNUMBER
MEMBERFUNCTIONodciaggregatemerge
ctx2INtype_str_agg
)RETURNNUMBER
)
/
CREATEORREPLACETYPEBODYtype_str_aggIS
RETURNNUMBERIS
BEGIN
sctx:
=type_str_agg(NULL);
RETURNodciconst.success;
END;
)RETURNNUMBERIS
SELF.total:
=SELF.total||VALUE;
returnvalue:
=SELF.total;
=SELF.total||ctx2.total;
END;
CREATEORREPLACEFUNCTIONf_stragg(p_inputVARCHAR2)RETURNVARCHAR2
PARALLEL_ENABLE
AGGREGATEUSINGtype_str_agg;
CREATEORREPLACEPACKAGEpkg_number_transIS
FUNCTIONf_bin_to_oct(p_strINVARCHAR2)RETURNVARCHAR2;
FUNCTIONf_bin_to_dec(p_strINVARCHAR2)RETURNVARCHAR2;
FUNCTIONf_bin_to_hex(p_strINVARCHAR2)RETURNVARCHAR2;
FUNCTIONf_oct_to_bin(p_strINVARCHAR2)RETURNVARCHAR2;
FUNCTIONf_oct_to_dec(p_strINVARCHAR2)RETURNVARCHAR2;
FUNCTIONf_oct_to_hex(p_strINVARCHAR2)RETURNVARCHAR2;
FUNCTIONf_hex_to_bin(p_strINVARCHAR2)RETURNVARCHAR2;
FUNCTIONf_hex_to_oct(p_strINVARCHAR2)RETURNVARCHAR2;
FUNCTIONf_hex_to_dec(p_strINVARCHAR2)RETURNVARCHAR2;
FUNCTIONf_dec_to_bin(p_intINVARCHAR2)RETURNVARCHAR2;
FUNCTIONf_dec_to_oct(p_intINVARCHAR2)RETURNVARCHAR2;
FUNCTIONf_dec_to_hex(p_intINVARCHAR2)RETURNVARCHAR2;
ENDpkg_number_trans;
CREATEORREPLACEPACKAGEBODYpkg_number_transIS
FUNCTIONf_bin_to_oct(p_strINVARCHAR2)RETURNVARCHAR2IS
----------------------------------------------------------------------------------------------------------------------
--对象名称:
f_bin_to_oct
--对象描述:
二进制转换八进制
--输入参数:
p_str二进制字符串
--返回结果:
八进制字符串
--测试用例:
SELECTpkg_number_trans.f_bin_to_oct('
11110001010'
)FROMdual;
--备注:
需要定义f_stragg函数和type_str_agg类型
v_returnVARCHAR2(4000);
v_binVARCHAR2(4000);
v_bin:
=substr('
00'
||p_str,-3*ceil(length(p_str)/3));
SELECTf_stragg(data1)INTOv_return
FROM(SELECT(CASEupper(substr(v_bin,(rownum-1)*3+1,3))
WHEN'
000'
THEN'
0'
001'
1'
010'
2'
011'
3'
100'
4'
101'
5'
110'
6'
111'
7'
END)data1
FROMdual
CONNECTBYrownum<
=length(v_bin)/3);
RETURNv_return;
EXCEPTION
WHENOTHERSTHEN
RETURNNULL;
ENDf_bin_to_oct;
FUNCTIONf_bin_to_dec(p_strINVARCHAR2)RETURNVARCHAR2IS
f_bin_to_dec
二进制转换十进制
十进制字符串
SELECTpkg_number_trans.f_bin_to_dec('
SELECTSUM(data1)INTOv_return
FROM(SELECTsubstr(p_str,rownum,1)*power(2,length(p_str)-rownum)data1
=length(p_str));
ENDf_bin_to_dec;
FUNCTIONf_bin_to_hex(p_strINVARCHAR2)RETURNVARCHAR2IS
f_bin_to_hex
二进制转换十六进制
十六进制字符串
||p_str,-4*ceil(length(p_str)/4));
FROM(SELECT(CASEupper(substr(v_bin,(rownum-1)*4+1,4))
0000'
0001'
0010'
0011'
0100'
0101'
0110'
0111'
1000'
8'
1001'
9'
1010'
A'
1011'
B'
1100'
C'
1101'
D'
1110'
E'
1111'
F'
=length(v_bin)/4);
ENDf_bin_to_hex;
FUNCTIONf_oct_to_bin(p_strINVARCHAR2)RETURNVARCHAR2IS
f_oct_to_bin
八进制转换二进制
p_str八进制字符串
二进制字符串
SELECTpkg_number_trans.f_oct_to_bin('
3612'
SELECTto_char(to_number(f_stragg(data1)))INTOv_return
FROM(SELECT(CASEupper(substr(p_str,rownum,1))
ENDf_oct_to_bin;
FUNCTIONf_oct_to_dec(p_strINVARCHAR2)RETURNVARCHAR2IS
f_oct_to_dec
八进制转换十进制
SELECTpkg_number_trans.f_oct_to_dec('
FROM(SELECTsubstr(p_str,rownum,1)*power(8,length(p_str)-rownum)data1
ENDf_oct_to_dec;
FUNCTIONf_oct_to_hex(p_strINVARCHAR2)RETURNVARCHAR2IS
八进制转换十六进制
SELECTpkg_number_trans.f_oct_to_hex('
SELECTp