比較經典的SQL PLSQL收集

2021-06-10 06:05:17 字數 4036 閱讀 2813

--pl/sql顯示哪個使用者(會話sid以及serial)通過什麼工具執行了什麼操作

declare

x number;

begin

for x in

( select username||'('||sid||','||serial#||

') ospid = ' || process ||

' program = ' || program username,

to_char(logon_time,' day hh24:mi') logon_time,

to_char(sysdate,' day hh24:mi') current_time,

sql_address, last_call_et

from v$session

where status = 'active'

and rawtohex(sql_address) <> '00'

and username is not null order by last_call_et )

loop

for y in ( select max(decode(piece,0,sql_text,null)) ||

max(decode(piece,1,sql_text,null)) ||

max(decode(piece,2,sql_text,null)) ||

max(decode(piece,3,sql_text,null))

sql_text

from v$sqltext_with_newlines

where address = x.sql_address

and piece < 4)

loop

if ( y.sql_text not like '%listener.get_cmd%' and

y.sql_text not like '%rawtohex(sql_address)%')

then

dbms_output.put_line( '--------------------' );

dbms_output.put_line( x.username );

dbms_output.put_line( x.logon_time || ' ' ||

x.current_time||

' last et = ' ||

x.last_call_et);

dbms_output.put_line(

substr( y.sql_text, 1, 250 ) );

end if;

end loop;

end loop;

end;

select username||'('||sid||','||serial#||')' username,

module,

action,

client_info

from v$session

where module||action||client_info is not null;

--取得兩個字串從第幾個字元開始不同。。。

select min(level) 

from dual

where nvl(substr('字串1',level,1),' ')<>nvl(substr('字串2',level,1),' ')

connect by level<=200;

--求兩個日期相差多少天、多少小時、多少分、多少秒

select extract(day

from(to_date('2012-12-31 12:11:12', 'yyyy-mm-dd hh24:mi:ss') -

to_date('2012-12-11 11:11:12', 'yyyy-mm-dd hh24:mi:ss'))

day(9) to second) || '天'||

extract(hour

from(to_date('2012-12-31 12:11:12', 'yyyy-mm-dd hh24:mi:ss') -

to_date('2012-12-11 11:11:12', 'yyyy-mm-dd hh24:mi:ss'))

day(9) to second) || '時'||

extract(minute

from(to_date('2012-12-31 12:11:12', 'yyyy-mm-dd hh24:mi:ss') -

to_date('2012-12-11 11:11:12', 'yyyy-mm-dd hh24:mi:ss'))

day(9) to second) || '分鐘'||

extract(second

from(to_date('2012-12-31 12:11:12', 'yyyy-mm-dd hh24:mi:ss') -

to_date('2012-12-11 11:11:12', 'yyyy-mm-dd hh24:mi:ss'))

day(9) to second) || '秒'

from dual

--求指定月內哪天在日曆表是對應哪周.

如:2013.04.01----2013.04.06為第一周,依次類推.

如果直接使用to_char(sysdate,'w')得到的週是按每個月的前7天為第一周的,這樣和日曆表上的有點出入.

select to_date('2013-04-01', 'yyyy-mm-dd') + level - 1,

ceil((to_char(to_date('2013-04-01', 'yyyy-mm-dd'), 'd') + level - 1) / 7) 周

from dual

connect by level <= 30;

--表t11有a,b兩個字段,a欄位為空,b欄位是26個字母無序排列

--要求更新a列,使得b='a'時,a=1;b='b'時,a=2;b='c'時,a=3...

update t11 c

set a =

(select rn

from (select rank() over(order by b) rn, b from t11) d

where d.b = c.b);

--sql完成字串拆分,拆分逗號分隔的字串

12:08:39 sys@orcl> select substr(col

12:08:43 2 ,decode(level, 1, 1, instr(col, ',', 1, level - 1) + 1)

12:08:43 3 ,decode(instr(col, ',', 1, level)

12:08:43 4 ,0

12:08:43 5 ,length(col) + 1

12:08:43 6 ,instr(col, ',', 1, level)) -

12:08:43 7 decode(level, 1, 1, instr(col, ',', 1, level - 1) + 1)) col

12:08:43 8 from (select '&a' col from dual)

12:08:43 9 connect by level <= length(translate(col, ',' || col, ',')) + 1;

輸入 a 的值: aa,bb,cc,dd,ee,ff

col----------------------------------

aabb

ccdd

eeff

已選擇6行。

隨機取表中第n條記錄

select * from 表 sample(10) where rownum = 1;

sql經典語句收集

1隨機取出10條資料 select top 10 from ma lot order bynewid 2.隨機選擇記錄 select newid 3.列出資料庫裡所有的表名 select name from sysobjects where type u u代表使用者 4.列出表裡的所有的列名 se...

前端經典問題收集

核心api doctype html lang en charset utf 8 name viewport content width device width,initial scale 1.0 documenttitle head ul const total 100000 插入十萬條資料 c...

經典SQL語句收集

選擇表的前10條 select top 10 from 表 order by 需要排序的字段 選擇表的11 20條 select from select row number over order by 需要排序的字段 as id,from 表 awhere id between 11 and 20...