--存储过程,循环
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. 帝国cms支持的变量及灵动标签变量汇总

    帝国CMS对首页.列表页.内容页这三个页面模板支持的变量是不同的,有的是通用的,有的不是通用的,本文就这三个模板常用的变量列于此,另外灵动标签很好用啊,也顺便收藏于此,以备后用,到时不用到处翻来翻去的 ...

  2. SSIS--(1)

    目标:两组数据比对,A 来源Excel ,B 来源 Sql server DB ,比对合并,取值放入目标 C 中 首先使用工具SSIS包 一,以数据源 A 为准核对B 中是否有A 的数据和计算等动作 ...

  3. if __name__==__main__:的应用

    1. if __name__==__main__:代码的作用 python的文件有两种使用的方法:(1)直接作为脚本执行:(2)import到其他的python脚本中被调用(模块重用)执行: 因此if ...

  4. XML 文档(1, 2)中有错误:不应有 <xml xmlns=''>

    症状 用XmlSerializer进行xml反序列化的时候,程序报错: 不应有 <xml xmlns=''>. 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息, ...

  5. 解决PHP5.6版本“No input file specified”的问题

    问题描述:使用TP框架做项目时,在启用REWRITE的伪静态功能的时候,首页可以访问,但是访问其它页面的时候,就提示:“No input file specified.”原因在于使用的PHP5.6是f ...

  6. Java实现个人博客网站

    说明:该项目是实验楼用户"LOU3165780622"发布在实验楼上的项目教程:[Java实现个人博客],未经允许,禁止转载: 该项目利用 SSM 框架和 Mysql 以及一些简单 ...

  7. C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作,无法为请求的 Configuration 对象创建配置文件。

    应用程序配置文件,对于asp.net是 web.config,对于WINFORM程序是 App.Config(ExeName.exe.config). 配置文件,对于程序本身来说,就是基础和依据,其本 ...

  8. FluentScheduler:开源轻量级定时任务调度架构

    安装:FluentScheduler Install-Package FluentScheduler 一.控制台中使用 using System; using System.Collections.G ...

  9. ShakaApktool 用法

    usage: ShakaApktool b[uild] [options] <app_path> -df,--default-framework 使用默认的框架资源文件. -f 跳过已编译 ...

  10. cocos2d 利用cocosStudio制作合图(plist与png)