--存储过程,循环
create or replace procedure delTables(ename t_emp.ename%TYPE)
AS
con number;
i NUMBER := 1;
tablename USER_TABLES.TABLE_NAME%TYPE;
BEGIN
 select count(TABLE_NAME) into con from USER_TABLES where last_analyzed > to_date('2014/1/17 00:00:00','yyyy/mm/dd hh24:mi:ss');
 while i<con
 loop
    DBMS_OUTPUT.put_line('循环 '||i||' 删除表 ');
    if i=1 then
   -- drop table user_session_online;--此存储过程loop里不可写sql语句
    DBMS_OUTPUT.put_line('删除表 '||' '||ename);
    end if;
    i := i + 1;
 end loop;
end;

begin
   delTables('sdf');
end;

--游标,循环
declare
   cursor mycur is select 'drop table '||TABLE_NAME ||';' from USER_TABLES where last_analyzed > to_date('2014/1/1 00:00:00','yyyy/mm/dd hh24:mi:ss');
   sqlStr varchar2(100);
   longStr varchar2(10000);
   i NUMBER :=0;
   begin
     open mycur;
     fetch mycur into sqlStr;
     while (mycur%found)  -- 判断是否有数据被发现
       loop
         i := i+1;
         dbms_output.put_line('编号: '||i||' '|| sqlStr);
         longStr := longStr||' '||sqlStr;
         --drop table E_PORTALDOCUMENT;--游标里遍历数据时不能写sql语句
         if i=10 then
         dbms_output.put_line('拼接 '|| longStr);--拼接字符串超出缓冲区
         end if;
         fetch mycur into sqlStr;--修改游标,继续向下
       end loop;
    end;

select * from t_emp
--带参数的游标  不能输入字符  end后要加;
declare
    empno t_emp.empno%TYPE;
    ename t_emp.ename%TYPE;
    cursor emp_cur(dep t_emp.empno%TYPE) is select empno,ename from t_emp where empno = dep;
    begin
      empno := &empno2;
      DBMS_OUTPUT.PUT_LINE(empno);
      open emp_cur(empno);
      loop
        fetch emp_cur into empno,ename;
        exit WHEN emp_cur%notfound;
        DBMS_OUTPUT.PUT_LINE(empno || ' ' || ename);
      end loop;
      close emp_cur;
   end;
     
--REF 游标       参数如果为字符串
  declare
     TYPE refcur_t is ref cursor;
     refcur refcur_t;
     selection number(1);
     emp t_emp%ROWTYPE;
  begin
     selection := &sel;
     dbms_output.put_line(selection);
     if selection = 0 then
        open refcur for select * from t_emp where empno = 4; 
     elsif selection = 1 then
        open refcur for select * from t_emp where empno = 2;     
     else
        dbms_output.put_line('***请输入0或1***'||emp.ename);
        open refcur for select * from t_emp where 0 = 1;
     end if;
        loop
            fetch refcur into emp;
            exit when refcur%notfound;
            dbms_output.put_line('******'||emp.ename);
        end loop;   
     close refcur;  
  end;

begin
for k in 1..10 loop
dbms_output.put_line(k);
end loop;
for k in reverse 1..10 loop
dbms_output.put_line(k);
end loop;
end;

declare
i binary_integer := 1;
begin
  loop
    dbms_output.put_line(i);
    i := i + 1;
    exit when ( i >= 11);
  end loop;
end;

declare
j binary_integer := 1;
begin
  while
    j < 11
    loop
      dbms_output.put_line(j);
      j := j + 1;
    end loop;
end;

create table emp (empno number(4),ename varchar2(10),job varchar2(9),mgr number(4),hiredate date,sal number(7,2),comm number(7,2),deptno number(2));
--员工编号,姓名,职位,领导编号,雇佣日期,工资,奖金,部门编号
select avg(mgr) from emp
--函数只能返回唯一值
create or replace function myFunction(nm varchar) return varchar
as
mg varchar(8);
begin
  select job into mg from emp where ename = nm;
  return mg;
end;

select myFunction('xmh1') from emp;
select avg(mgr) from emp where ename ='xmh1'

create or replace function myFunc(nm NUMBER) return NUMBER
as
mg NUMBER;
begin
  select empno into mg from emp where mgr=2;
  return mg;
end;

select distinct myFunc(1) from emp;

select object_name,status from user_objects where object_type='FUNCTION';

declare
  eno emp.empno%TYPE;
  empInfo emp%ROWTYPE;
  begin
    eno :=&en;
    select * into empInfo from emp where empno = eno;
    DBMS_OUTPUT.put_line('雇员编号:'||empInfo.empno);
    DBMS_OUTPUT.put_line('雇员姓名:'||empInfo.ename);
    end;
   
--游标
declare
   mg emp.mgr%TYPE;
   cursor mycur is select * from emp where MGR = mg;
   empInfo emp%ROWTYPE;--变量 定义类型 把一行的数据都装进来
   cou NUMBER;
   empnum emp.empno%TYPE;
   BEGIN
     empnum := &empnum;-- 输入框 输入参数
     mg := &mg;
     for empInfo in mycur LOOP--在游标里循环 获取empInfo
       cou := mycur%ROWCOUNT;
       DBMS_OUTPUT.put_line(cou||'雇员编号:'||empInfo.empno||'  '||empnum);
       if empInfo.job='job3' then
       DBMS_OUTPUT.put_line(cou||'雇员姓名:'||empInfo.ename);
       end if;
     END LOOP;
     END;

declare
   cursor mycur is select * from emp;
   empInfo emp%rowtype;
   begin
     open mycur;
     fetch mycur into empInfo;
     while (mycur%found) loop -- 判断是否有数据被发现
       dbms_output.put_line('编号: '|| empInfo.empno);
    
       fetch mycur into empInfo;--修改游标,继续向下
       end loop;
      end;
declare
   cursor mycur is select * from emp;
   empInfo emp%rowtype;
   rown number;
   begin
     open mycur;
     fetch mycur into empInfo;
     if (mycur%found) then -- 判断是否有数据被发现
       DBMS_OUTPUT.put_line(rown||'编号: '|| empInfo.empno);
       fetch mycur into empInfo;
       end if;
      end;   
       
--存储过程    sqlplus 用 exec 执行过程
create or replace procedure myproc(eno emp.empno%TYPE,enm emp.Ename%TYPE,job emp.job%TYPE,mgr emp.mgr%TYPE)
AS
con NUMBER ;
BEGIN
  select count(empno) into con from emp where empno = eno;
  if con = 0 then
    insert into emp(empno,ename,job,mgr) values (eno,enm,job,mgr);
    DBMS_OUTPUT.put_line('插入成功!');
  else
    DBMS_OUTPUT.put_line('已存在!');
  end if;  
end;

select count(empno)  from emp where empno =0;
select * from emp;
begin 
  myproc(05,'xmh5','business',1);
end;
--exec myproc(05,'xmh5','business',1);

create or replace procedure myproc
as
i number;
begin
  i:=100;
  DBMS_OUTPUT.put_line('i= '||i);
end;

create or replace procedure myproc(eno emp.empno%TYPE)
as
i number;
begin
  --insert into EMP (empno) values (eno);
  --delete from emp where empno=eno;
  update emp set ename = 'xmh333' where empno=eno;
  --select empno into i from emp where empno=eno;
  --DBMS_OUTPUT.put_line('OUTPUT: '||i);
end;

begin
 myproc(12);
end; 
(05,'xmh5','business',1)

SELECT * FROM EMP;

declare
m number(6);
begin
m:=&请输入一个数字作为变量m的值;
dbms_output.put_line(m);
end;

--触发器
create or replace trigger tr_src_emp
before insert or update or delete on emp
begin
  if to_char(sysdate,'DY')in('星期四','星期六')then
    raise_application_error(-20001,'fail');
    end if;
    end;
insert into emp (empno) values (12);

select username,default_tablespace from user_users
select * from all_users;

select * from all_tables where tablespace_name like 'TEST_DATA'
select * from all_tables where table_name like '%ULTRA%';
select * from ULTRA_XMH_TEST;
select * from user_sys_privs;
select * from dba_sys_privs

--创建临时表空间
create temporary tablespace test_temp
tempfile 'E:\oracle\product\10.2.0\oradata\orcl\test_temp01.dbf'
size 32m
autoextend on
next 32m maxsize 2048m
extent management local;
 
--创建数据表空间
create tablespace test_data
logging
datafile 'E:\oracle\product\10.2.0\oradata\orcl\test_data01.dbf'
size 32m
autoextend on
next 32m maxsize 2048m
extent management local;
 
--创建用户并指定表空间
create user xmh identified by xmh
default tablespace test_data
temporary tablespace test_temp;

(1)创建用户
 Create user 用户名 identified by 密码;(如果是纯数字则要加双引号”654321”,如果是字母就不用)

(2)授权给某个用户
 Grant connect,resource to 用户名;(只有用户有了connect 和 resource后才能操作其他表)

(3)授DBA 权限化
 Grant dba to 用户名;

(4)给用户创建会话的权限:

grant create session to DB_USER

(5)撤权:  
 revoke   权限...   from 用户名;

(6)删除用户:
 drop user username cascade (cascade 保证彻底删除)

oracle 游标/函数/存储过程/触发器 表空间的更多相关文章

  1. 关于oracle的函数,存储过程,触发器,序列,视图,左右连接一些的应用 带案例

    CREATE TABLE STUDENT( --创建学生表  ID NUMBER(10) PRIMARY KEY,   --主键ID  NAME VARCHAR2(20),  CLASSNAME VA ...

  2. Oracle 数据库、实例、表空间、用户、数据库对象

    Oracle是一种数据库管理系统,是一种关系型的数据库管理系统.通常情况了我们称的“数据库”,包含了物理数据.数据库管理系统.内存.操作系统进程的组合体,就是指这里所说的数据库管理系统. 完整的Ora ...

  3. oracle入坑日记<四>表空间

    1   表空间是什么 1.1.数据表看做的货品,表空间就是存放货品的仓库.SQLserver 用户可以把表空间看做 SQLserver 中的数据库. 1.2.引用[日记二]的总结来解释表空间. 一个数 ...

  4. Oracle基础(三) 表空间

    数据库的存储结构 数据库主要用于存储和检索相关的信息,Oracle数据库包含逻辑结构和物理结构. 物理结构是指现实存储单元,由一组文件组成如数据文件.日志文件.控制文件. 数据文件:用于存储数据的文件 ...

  5. 【转】Oracle - 数据库的实例、表空间、用户、表之间关系

    [转]Oracle - 数据库的实例.表空间.用户.表之间关系 完整的Oracle数据库通常由两部分组成:Oracle数据库和数据库实例. 1) 数据库是一系列物理文件的集合(数据文件,控制文件,联机 ...

  6. oracle 查询表的大小,表空间的使用情况,默认表空间

    oracle 查询表的大小,表空间的使用情况,默认表空间 oracle 查询表的大小,表空间的使用情况,默认表空间 --查看某张表占用磁盘空间大小 ( 表名大写 ) Select Segment_Na ...

  7. --使用oracle数据先要创建表空间

    one\--创建表空间 CREATE TABLESPACE 表空间的名字DATAFILE 'E:\oracle\app\userdata\java5space.dbf' --表空间物理文件路径SIZE ...

  8. 转 Oracle Transportable TableSpace(TTS) 传输表空间 说明

    ############1   迁移数据库的集中方法 三.相关技术 迁移方式 优势 不足1 Export and import • 对数据库版本,以及系统平台没有要求 • 不支持并发,速度慢• 停机时 ...

  9. oracle删除非空的表空间

    oracle删除非空的表空间: drop tablespace tablespaceName including contents;

随机推荐

  1. lsof 命令

    [root@localhost ~]# lsof COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME init root cwd DIR , / in ...

  2. 谷歌浏览器安装octotree插件

    Octotree Chrome安装与使用方法 Octotree Chrome作用: 主要使你在github查看项目时可以清晰明了的看到项目的结构以及具体代码,使下载代码更具有目的性,减少不必要代码的下 ...

  3. socket 套接字总结

    简单版 服务端 import socket import struct import json import os server_dir = r'E:\Moudule_1\socket练习\serve ...

  4. SQL——查询一段时间内每天的数据,按天将数据封装进行封存

    DROP TABLE IF EXISTS `T_ROTA_RECORD`; CREATE TABLE `T_ROTA_RECORD` ( `id` ) NOT NULL AUTO_INCREMENT, ...

  5. sap gui 定义类并实现接口

    1: 直接在类属性的interfaces 框输入 接口名称即可.

  6. [django]form的content-type(mime)

    form默认的content-type是 'application/x-www-form-urlencoded' 可以修改为多文档: enctype即为mime类型 <form action=& ...

  7. Day2 Python基础学习——字符串、列表、元组、字典、集合

    Python中文学习大本营:http://www.pythondoc.com/ 一.字符串操作 一.用途:名字,性格,地址 name = 'wzs' #name = str('wzs')print(i ...

  8. 使用autoconf与automake自动生成MakeFile文件

    automake主要通过编辑Makefile.am来控制它的行为,下面就常用的三个Makefile.am配置做出说明. 1.1. autotools的工作原理 autotools最终是为了生成Make ...

  9. 【设计模式】用追MM来解释23种设计模式,转

    创建型模式 1.FACTORY—追MM少不了请吃饭了,麦当劳的鸡翅和肯德基的鸡翅都是MM爱吃的东西,虽然口味有所不同,但不管你带MM去麦当劳或肯德基,只管向服务员说“来四个鸡翅”就行了.麦当劳和肯德基 ...

  10. bootstrap modal与select2使用冲突解决

    今天发现项目使用bootstrap modal 与 jquery select2 结合时发现select2不起作用,点击select框不显示选项,查阅资料后发现是因为modal层遮挡了select2的 ...