Oracle 数据库特殊查询总结

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;

8.完了。

时间: 2024-10-08 09:07:52

Oracle 数据库特殊查询总结的相关文章

ORACLE数据库常用查询二

ORACLE数据库常用查询 1.查看表空间对应数据文件情况: SQL> SELECT TABLESPACE_NAME,FILE_NAME,BYTES/1024/1024 MB,AUTOEXTENSIBLE FROM DBA_DATA_FILES; TABLESPACE_NAME FILE_NAME MB AUTOEXTENSIBLE ------------------------------ ---------------------------------------------------

Oracle数据库对象查询是否被锁定及解锁

Oracle数据库对象是否被锁定查询SQL及解锁过程SQL详见下面: --查询当前用户下被锁的对象SELECT B.OBJECT_NAME, B.OWNER, C.SID,C.SERIAL#,C.OSUSER, C.MACHINE, C.TERMINAL  FROM V$LOCKED_OBJECT A, DBA_OBJECTS B, V$SESSION C WHERE A.OBJECT_ID = B.OBJECT_ID   AND A.SESSION_ID = C.SID   AND c.STA

MySQL数据库分页查询,Oracle数据库分页查询,SqlServer数据库分页

DROP TABLE IF EXISTS `student`; CREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `pass` varchar(255) NOT NULL, `sex` varchar(255) NOT NULL, `age` int(11) NOT NULL, `address` varchar(255) DEFAULT NULL, PRIMA

Oracle数据库,查询语句、内置函数

一.数据库的查询语句: 1.查询整个表: select * from 表名 例: 2.通过条件查询某一行数据: select * from 表名 where 字段名 例: 3.某一列数据去重查询: select distinct 字段名 from 表名 例: 4.查询的结果按某个字段升序或倒序排列:  select * from 表名 order by 字段名;                  在字段名的后面加desc为降序顺序排列 例: 5.查询某一列在某个范围内的数据: select *

从Oracle数据库中查询前几个月数据时需要注意的一些问题

在最近的一个项目中,有一个需求就是要查询数据库中前几个月的历史数据,但是由于自己考虑不全面造成了程序的bug,现在将这一块好好作一个总结,希望以后不再犯这种很低级的错误,首先贴出查询中用到的一个子函数,然后就此作出详细的分析: private string AddMonths(string originalTime, int months) { string returnString = string.Empty; string[] dataAndTime = originalTime.Spli

oracle数据库字符集查询

1>数据库服务器字符集 select * from nls_database_parameters,其来源于props$,是表示数据库的字符集. 查询结果如下 NLS_LANGUAGE AMERICANNLS_TERRITORY AMERICANLS_CURRENCY $NLS_ISO_CURRENCY AMERICANLS_NUMERIC_CHARACTERS .,NLS_CHARACTERSET AL32UTF8NLS_CALENDAR GREGORIANNLS_DATE_FORMAT DD

关于sql server远程访问Oracle数据库 OpenQuery查询返回多条数据的问题

在Sql Server远程访问Oracle 中的数据库表时: 远程语法通常为: select * from OpenQuery(Oracle链接服务器名称,‘查询语句’) eg: select * from OPENQUERY(QTX,'select * from student') 有些情况下只会返回student表一条数据 第一条 解决方法:数据源ODBC中 选择系统DNS 时,新建系统数据源 选择的对应驱动应该为Oracle Instant Client Dricver  这个驱动需要安装

oracle 数据库信息查询

/*查询当前用户表信息/ select A.column_name    字段名, A.data_type      数据类型, A.data_length    长度, A.data_precision 整数位, A.Data_Scale     小数位, A.nullable       允许空值, A.Data_default   缺省值, B.comments       备注 from user_tab_columns A, user_col_comments B where A.Ta

oracle数据库子查询的结果需要使用多次解决办法

with c as (select a.trandt,sum(a.tranam) tranam from tran a group by a.trandt )--将子查询抽取出来,以后可以直接用.该方法只适用于oracle,mysql不支持 select c.trandt, sum(d.tranam) from c inner join c d on c.trandt >= d.trandt group by c.trandt select c.trandt, sum(d.tranam) fro