oracle 游标/函数/存储过程/触发器 表空间

 

--存储过程,循环
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 保证彻底删除)

 

posted @ 2015-01-27 14:51  苍天一穹  阅读(319)  评论(0编辑  收藏  举报