Oracle 常用pl/sql脚本

--设置语言
ALTER SESSION SET nls_language = 'american'


--查找职责或请求组
select frv.RESPONSIBILITY_NAME,
       frg.request_group_name,
       frg.request_group_code,
       fcpv.user_concurrent_program_name,
       --favg.APPLICATION_NAME req_app_name,
       favc.APPLICATION_NAME con_app_name
  from FND_REQUEST_GROUPS frg,
       FND_REQUEST_GROUP_UNITS frgu,
       FND_CONCURRENT_PROGRAMS_VL fcpv,
       --FND_APPLICATION_VL favg,
       FND_APPLICATION_VL favc,
       FND_RESPONSIBILITY_VL frv
 where frg.REQUEST_GROUP_ID = frgu.REQUEST_GROUP_ID
   and frg.application_id = frgu.application_id
   and fcpv.CONCURRENT_PROGRAM_ID = frgu.request_unit_id
   --and fcpv.CONCURRENT_PROGRAM_ID = 48023
   --and frg.application_id = favg.application_id
   and fcpv.application_id = favc.application_id
   --and fcpv.concurrent_program_name like 'Create Consumption Advice%'
   AND fcpv.user_concurrent_program_name like 'Create Consumption Advice%'
   --AND favc.APPLICATION_NAME like 'Create Consumption Advice%'
   and fcpv.enabled_flag = 'Y'
   and frv.REQUEST_GROUP_id(+) = frg.REQUEST_GROUP_ID


--查找消息内容
select message_text, message_number, type, fnd_log_severity, category, severity
            from fnd_new_messages m, fnd_application a
            where 1=1
            and m.application_id = a.application_id
            AND NAME_ARG = m.message_name
            and LANG_ARG = m.language_code
            and APPIN = a.application_short_name

 

--在Oracle中查看所有的表:
select * from tab/dba_tables/dba_objects/cat;

--看用户建立的表   : 
select table_name from user_tables;  //当前用户的表  
   
select table_name from all_tables;  //所有用户的表  
   
select table_name from dba_tables;  //包括系统表

--automatically set your operating unit
dbms_application_info.set_client_info('&Org_id');

--查看所有package和package body
SELECT  DISTINCT NAME  FROM   ALL_SOURCE  
  WHERE   TYPE   =   'PACKAGE'   AND  
  OWNER   =   'APPS'  AND
  NAME LIKE 'XX%'
--  ROWNUM <10 
  ORDER   BY   NAME;


--用户授权:
GRANT ALTER ANY INDEX TO "user_id "
GRANT "dba " TO "user_id ";
ALTER USER "user_id " DEFAULT ROLE ALL

--创建用户:
CREATE USER "user_id " PROFILE "DEFAULT " IDENTIFIED BY " DEFAULT TABLESPACE "USERS " TEMPORARY TABLESPACE "TEMP " ACCOUNT UNLOCK;
GRANT "CONNECT " TO "user_id ";

--用户密码设定:
ALTER USER "CMSDB " IDENTIFIED BY "pass_word "

--表空间创建:
CREATE TABLESPACE "table_space " LOGGING DATAFILE 'C:\ORACLE\ORADATA\dbs\table_space.ora' SIZE 5M


------------------------------------------------------------------------

--1、查看当前所有对象
SQL > select * from tab;


--2、建一个和a表结构一样的空表
SQL > create table b as select * from a where 1=2;
SQL > create table b(b1,b2,b3) as select a1,a2,a3 from a where 1=2;


--3、察看数据库的大小,和空间使用情况
SQL > col tablespace format a20
SQL > select b.file_id  文件ID,
  b.tablespace_name  表空间,
  b.file_name     物理文件名,
  b.bytes       总字节数,
  (b.bytes-sum(nvl(a.bytes,0)))   已使用,
  sum(nvl(a.bytes,0))        剩余,
  sum(nvl(a.bytes,0))/(b.bytes)*100 剩余百分比
  from dba_free_space a,dba_data_files b
  where a.file_id=b.file_id
  group by b.tablespace_name,b.file_name,b.file_id,b.bytes
  order by b.tablespace_name
  /
  dba_free_space --表空间剩余空间状况
  dba_data_files --数据文件空间占用情况

 

--4、查看现有回滚段及其状态
SQL > col segment format a30
SQL > SELECT SEGMENT_NAME,OWNER,TABLESPACE_NAME,SEGMENT_ID,FILE_ID,STATUS FROM DBA_ROLLBACK_SEGS;


--5、查看数据文件放置的路径
SQL > col file_name format a50
SQL > select tablespace_name,file_id,bytes/1024/1024,file_name from dba_data_files order by file_id;


--6、显示当前连接用户
SQL > show user


--7、把SQL*Plus当计算器
SQL > select 100*20 from dual;


--8、连接字符串
SQL > select 列1 | |列2 from 表1;
SQL > select concat(列1,列2) from 表1;


--9、查询当前日期
SQL > select to_char(sysdate,'yyyy-mm-dd,hh24:mi:ss') from dual;


--10、用户间复制数据
SQL > copy from user1 to user2 create table2 using select * from table1;


--11、视图中不能使用order by,但可用group by代替来达到排序目的
SQL > create view a as select b1,b2 from b group by b1,b2;


--12、通过授权的方式来创建用户
SQL > grant connect,resource to test identified by test;
SQL > conn test/test


--13、查出当前用户所有表名。
select unique tname from col;


-----------------------------------------------------------------------

/* 向一个表格添加字段 */
alter table alist_table add address varchar2(100);


/* 修改字段 属性 字段为空 */
alter table alist_table modify address varchar2(80);


/* 修改字段名字 */
create table alist_table_copy as select ID,NAME,PHONE,EMAIL,
QQ as QQ2, /*qq 改为qq2*/
ADDRESS from alist_table;

drop table alist_table;

rename alist_table_copy to alist_table

/* 修改表名 */


--空值处理
--有时要求列值不能为空
create table dept (deptno number(2) not null, dname char(14), loc char(13));


--在基表中增加一列
alter table dept
add (headcnt number(3));


--修改已有列属性
alter table dept
modify dname char(20);

/*注:只有当某列所有值都为空时,才能减小其列值宽度。
只有当某列所有值都为空时,才能改变其列值类型。
只有当某列所有值都为不空时,才能定义该列为not null。*/
--例:
alter table dept modify (loc char(12));
alter table dept modify loc char(12);
alter table dept modify (dname char(13),loc char(12));


--查找未断连接
select process,osuser,username,machine,logon_time ,sql_text
from v$session a,v$sqltext b where a.sql_address=b.address;


-----------------------------------------------------------------

--1.以USER_开始的数据字典视图包含当前用户所拥有的信息, 查询当前用户所拥有的表信息:
select * from user_tables;

/*2.以ALL_开始的数据字典视图包含ORACLE用户所拥有的信息,
查询用户拥有或有权访问的所有表信息: */
select * from all_tables;


--3.以DBA_开始的视图一般只有ORACLE数据库管理员可以访问:
select * from dba_tables;


--4.查询ORACLE用户:
conn sys/change_on_install
select * from dba_users;
conn system/manager;
select * from all_users;


--5.创建数据库用户:
CREATE USER user_name IDENTIFIED BY password;
GRANT CONNECT TO user_name;
GRANT RESOURCE TO user_name;

--授权的格式: grant (权限) on tablename to username;
--删除用户(或表):
drop user(table) username(tablename) (cascade);

--6.向建好的用户导入数据表
IMP SYSTEM/MANAGER FROMUSER = FUSER_NAME TOUSER = USER_NAME FILE = C:\EXPDAT.DMP COMMIT = Y

--7.索引
create index [index_name] on [table_name]( "column_name ")


--输出
dbms_output.put_line('lv_object_version_number : '||lv_object_version_number);

 

--oracle中的userenv及sys_context的参数2007-12-21 16:451、 USERENV(OPTION)
  返回当前的会话信息.
  OPTION='ISDBA'若当前是DBA角色,则为TRUE,否则FALSE.
  OPTION='LANGUAGE'返回数据库的字符集.
  OPTION='SESSIONID'为当前会话标识符.
  OPTION='ENTRYID'返回可审计的会话标识符.
  OPTION='LANG'返回会话语言名称的ISO简记.
  OPTION='INSTANCE'返回当前的实例.
OPTION='terminal'返回当前计算机名
  SELECT USERENV('LANGUAGE') FROM DUAL;

2、sys_context
select
SYS_CONTEXT('USERENV','TERMINAL') terminal,
SYS_CONTEXT('USERENV','LANGUAGE') language,
SYS_CONTEXT('USERENV','SESSIONID') sessionid,
SYS_CONTEXT('USERENV','INSTANCE') instance,
SYS_CONTEXT('USERENV','ENTRYID') entryid,
SYS_CONTEXT('USERENV','ISDBA') isdba,
SYS_CONTEXT('USERENV','NLS_TERRITORY') nls_territory,
SYS_CONTEXT('USERENV','NLS_CURRENCY') nls_currency,
SYS_CONTEXT('USERENV','NLS_CALENDAR') nls_calendar,
SYS_CONTEXT('USERENV','NLS_DATE_FORMAT') nls_date_format,
SYS_CONTEXT('USERENV','NLS_DATE_LANGUAGE') nls_date_language,
SYS_CONTEXT('USERENV','NLS_SORT') nls_sort,
SYS_CONTEXT('USERENV','CURRENT_USER') current_user,
SYS_CONTEXT('USERENV','CURRENT_USERID') current_userid,
SYS_CONTEXT('USERENV','SESSION_USER') session_user,
SYS_CONTEXT('USERENV','SESSION_USERID') session_userid,
SYS_CONTEXT('USERENV','PROXY_USER') proxy_user,
SYS_CONTEXT('USERENV','PROXY_USERID') proxy_userid,
SYS_CONTEXT('USERENV','DB_DOMAIN') db_domain,
SYS_CONTEXT('USERENV','DB_NAME') db_name,
SYS_CONTEXT('USERENV','HOST') host,
SYS_CONTEXT('USERENV','OS_USER') os_user,
SYS_CONTEXT('USERENV','EXTERNAL_NAME') external_name,
SYS_CONTEXT('USERENV','IP_ADDRESS') ip_address,
SYS_CONTEXT('USERENV','NETWORK_PROTOCOL') network_protocol,
SYS_CONTEXT('USERENV','BG_JOB_ID') bg_job_id,
SYS_CONTEXT('USERENV','FG_JOB_ID') fg_job_id,
SYS_CONTEXT('USERENV','AUTHENTICATION_TYPE') authentication_type,
SYS_CONTEXT('USERENV','AUTHENTICATION_DATA') authentication_data
from dual

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/14152023/viewspace-589234/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/14152023/viewspace-589234/

你可能感兴趣的:(Oracle 常用pl/sql脚本)