Oracle 数据库特殊查询总结
程序员文章站
2023-02-19 21:15:59
1. 查询本节点及本节点以下的所有节点:
select * from table1 c start with c.p_id='0000000' connect b...
1. 查询本节点及本节点以下的所有节点:
select * from table1 c start with c.p_id='0000000' connect by prior c.id=c.p_id and c.use_yn='y' order by id ;
2. 查询节点中所有的层级关系
select rpad( ' ', 2*(level-1), '-' ) || depname "depname",connect_by_root depname "root",connect_by_isleaf "isleaf",level ,sys_connect_by_path(depname, '/') "path" from dep start with upperdepid is null connect by prior depid = upperdepid; 1> connect_by_root 返回当前节点的最顶端节点 2> connect_by_isleaf 判断是否为叶子节点,如果这个节点下面有子节点,则不为叶子节点 3> level 伪列表示节点深度 4> sys_connect_by_path函数显示详细路径,并用“/”分隔
3. 对数据库表结构的操作
alter table taxasset add (nextdate varchar2(30)); alter table tax_dep_manager modify fddbrxm varchar2(120); alter table test1 drop column name;
4. 其他查询
/*用户被占用的查询*/ select 'alter system kill session '''||sid||','||serial#||''';' from v$session where username = 'users'; /* 系统数据库相关查询 */ select * from user_tablespaces; select username,default_tablespace from dba_users where username='zzs' select count(*) from user_views; --yb53 zzs 53 select count(*) from user_tables; --yb413 zzs 413 --查询表空间使用情况 select upper(f.tablespace_name) "表空间名",d.tot_grootte_mb "表空间大小(m)",d.tot_grootte_mb - f.total_bytes "已使用空间(m)", to_char(round(( d.tot_grootte_mb - f.total_bytes ) / d.tot_grootte_mb * 100, 2), '990.99')|| '%' "使用比",f.total_bytes "空闲空间(m)", f.max_bytes "最大块(m)" from (select tablespace_name,round(sum(bytes) / ( 1024 * 1024 ), 2) total_bytes,round(max(bytes) / ( 1024 * 1024 ), 2) max_bytes from sys.dba_free_space group by tablespace_name) f, (select dd.tablespace_name, round(sum(dd.bytes) / ( 1024 * 1024 ), 2) tot_grootte_mb from sys.dba_data_files dd group by dd.tablespace_name) d where d.tablespace_name = f.tablespace_name order by 1 --查询表空间的free space select tablespace_name, count(*) as extends, round(sum(bytes) / 1024 / 1024, 2) as mb, sum(blocks) as blocks from dba_free_space group by tablespace_name; --查询表空间的总容量 select tablespace_name, sum(bytes) / 1024 / 1024 as mb from dba_data_files group by tablespace_name; --表空间容量查询 select tablespace_name "表空间", to_char(round(bytes / 1024, 2), '99990.00') || '' "实有", to_char(round(free / 1024, 2), '99990.00') || 'g' "现有", to_char(round(( bytes - free ) / 1024, 2), '99990.00') || 'g' "使用", to_char(round(10000 * used / bytes) / 100, '99990.00') || '%' "比例" from (select a.tablespace_name tablespace_name, floor(a.bytes / ( 1024 * 1024 )) bytes, floor(b.free / ( 1024 * 1024 )) free, floor(( a.bytes - b.free ) / ( 1024 * 1024 )) used from (select tablespace_name tablespace_name, sum(bytes) bytes from dba_data_files group by tablespace_name) a, (select tablespace_name tablespace_name, sum(bytes) free from dba_free_space group by tablespace_name) b where a.tablespace_name = b.tablespace_name) order by floor(10000 * used / bytes) desc;
6. loop 的使用
declare con number; begin con :=1; loop dbms_output.put_line(con); con:=con+1; exit when con>100; end loop; dbms_output.put_line('完了'); end;
7. 存储过程的书写
create or replace procedure insertbranch(tablename in varchar2) as counts number; num number; begin create table tempdata (column1 nvarchar2,column2 nvarchar2,column3 nvarchar2); insert tempdata num := 1; select count(*) into counts from tablename; dbms_output.put_line('数据总数'+counts); while num <= counts loop dbms_output.put_line('循环开始:'); dbms_output.put_line('第'+num+'条数据'); select column1 into column1 from (select tablename.*, rownum as con from tablename) where con = num; select column2 into column2 from (select tablename.*, rownum as con from tablename) where con = num; select column3 into column3 from (select tablename.*, rownum as con from tablename) where con = num; insert into com_department values (brno, brname, upbrno, upbrno, 'n', null, null, null, '1', null, 'y', '2', null, null, null, 2, 'n', null, null, null, 'n', brno, upbrno, null, null, null, 'a', 'n', 'n', 0, 0, 3, null, null, null, '0', '0', 0, null, null, null, null, null, null, null); num := num + 1; end loop; end;
以上所述是小编给大家介绍的oracle 数据库特殊查询总结,希望对大家有所帮助!