第一部分
[Q]怎麼樣查詢特殊字符
[A]select * from table where name like
[Q]如何插入單引號到數據庫表中
[A]可以用ASCII碼處理
insert into t values(
或者用兩個單引號表示一個
or insert into t values(
[Q]怎樣設置事務一致性
[A]set transaction [isolation level] read committed; 默認語句級一致性
set transaction [isolation level] serializable;
read only; 事務級一致性
[Q]怎麼樣利用游標更新數據
[A]cursor c
select * from tablename
where name is null for update [of column]
……
update tablename set column = ……
where current of c
[Q]怎樣自定義異常
[A] pragma_exception_init(exception_name
如果立即拋出異常
raise_application_error(error_number
其中number從
異常變量
SQLCODE 錯誤代碼
SQLERRM 錯誤信息
[Q]十進制與十六進制的轉換
[A]
to_char(
to_number(
create or replace function to_base( p_dec in number
return varchar
is
l_str varchar
l_num number default p_dec;
l_hex varchar
begin
if ( p_dec is null or p_base is null ) then
return null;
end if;
if ( trunc(p_dec) <> p_dec OR p_dec <
raise PROGRAM_ERROR;
end if;
loop
l_str := substr( l_hex
l_num := trunc( l_num/p_base );
exit when ( l_num =
end loop;
return l_str;
end to_base;
/
create or replace function to_dec
( p_str in varchar
p_from_base in number default
is
l_num number default
l_hex varchar
begin
if ( p_str is null or p_from_base is null ) then
return null;
end if;
for i in
l_num := l_num * p_from_base + instr(l_hex
end loop;
return l_num;
end to_dec;
/
[Q]能不能介紹SYS_CONTEXT的詳細用法
[A]利用以下的查詢
select
SYS_CONTEXT(
SYS_CONTEXT(
SYS_CONTEXT(
SYS_CONTEXT(
SYS_CONTEXT(
SYS_CONTEXT(
SYS_CONTEXT(
SYS_CONTEXT(
SYS_CONTEXT(
SYS_CONTEXT(
SYS_CONTEXT(
SYS_CONTEXT(
SYS_CONTEXT(
SYS_CONTEXT(
SYS_CONTEXT(
SYS_CONTEXT(
SYS_CONTEXT(
SYS_CONTEXT(
SYS_CONTEXT(
SYS_CONTEXT(
SYS_CONTEXT(
SYS_CONTEXT(
SYS_CONTEXT(
SYS_CONTEXT(
SYS_CONTEXT(
SYS_CONTEXT(
SYS_CONTEXT(
SYS_CONTEXT(
SYS_CONTEXT(
from dual
[Q]怎麼獲得今天是星期幾
[A]可以用to_char來解決
select to_char(to_date(
在獲取之前可以設置日期語言
ALTER SESSION SET NLS_DATE_LANGUAGE=
還可以在函數中指定
select to_char(to_date(
其它更多用法
如獲得完整的時間格式
select to_char(sysdate
隨便介紹幾個其它函數的用法
本月的天數
SELECT to_char(last_day(SYSDATE)
今年的天數
select add_months(trunc(sysdate
下個星期一的日期
SELECT Next_day(SYSDATE
[Q]隨機抽取前N條記錄的問題
[A]
select * from (select * from tablename order by sys_guid()) where rownum < N;
select * from (select * from tablename order by dbms_random
注
dbms_random
[Q]抽取從N行到M行的記錄
[A]select * from (select rownum id
and rownum <=
[Q]怎麼樣抽取重復記錄
[A]select * from table t
(select max(rowed) from table t
where t
或者
select count(*)
group by col_a
having count(*)>
如果想刪除重復記錄
[Q]怎麼樣設置自治事務
[A]
pragma autonomous_transaction;
……
commit|rollback;
[Q]怎麼樣在過程中暫停指定時間
[A]DBMS_LOCK包的sleep過程
如
[Q]怎麼樣快速計算事務的時間與日志量
[A]可以采用類似如下的腳本
DECLARE
start_time NUMBER;
end_time NUMBER;
start_redo_size NUMBER;
end_redo_size NUMBER;
BEGIN
start_time := dbms_utility
SELECT VALUE INTO start_redo_size FROM v$mystat m
WHERE m
AND s
INSERT INTO t
SELECT * FROM All_Objects;
COMMIT;
end_time := dbms_utility
SELECT VALUE INTO end_redo_size FROM v$mystat m
WHERE m
AND s
dbms_output
dbms_output
END;
[Q]怎樣創建臨時表
[A]
create global temporary tablename(column list)
on commit preserve rows;
on commit delete rows;
臨時表是相對於會話的
[Q]怎麼樣在PL/SQL中執行DDL語句
[A]
execute immediate sql;
dbms_utility
[Q]怎麼樣獲取IP地址
[A]服務器(
客戶端
[Q]怎麼樣加密存儲過程
[A]用wrap命令
wrap iname=a
PL/SQL Wrapper: Release
From:http://tw.wingwit.com/Article/program/Oracle/201311/16737.html