oracle随笔

ぃ、小莉子 提交于 2019-11-28 15:06:19
  • 检查所有数据表的拥有者:owner 为SCHEMA,table_name 用户拥有的表格名称
select owner,table_name from all_tables
  • 分配函数funtion给其他用户使用
grant execute on funtionname to username
  • 查询表空间数据,是否自动增长
SELECT tablespace_name,file_name,autoextensible
  FROM dba_data_files
 WHERE tablespace_name = 'SCHEMA';
View Code
  • 查询表格的字段属性
SELECT * FROM all_tab_columns WHERE table_name = 'TABLENAME'
  • 查询当前用户所有数据表
SELECT table_name FROM user_tables
  • 查询所有数据表及字段名,可以通过知道某个字段的名称,查找应用的数据表
SELECT t.column_name, t.column_name FROM user_col_comments
  • 查询指定表的所有字段名
SELECT column_name
  FROM user_col_comments 
 WHERE table_name = 'tablename';
View Code
  • 查询所有表的表名和表说明
SELECT t.table_name, f.comments
  FROM user_tables t
 INNER JOIN user_tab_comments f
    ON t.table_name = f.table_name;
View Code
  • 查询模糊表名的表名和表说明
SELECT t.table_name, f.comments
  FROM user_tables t
 INNER JOIN user_tab_comments f
    ON t.table_name = f.table_name
 WHERE t.table_name LIKE 'BIZ_DICT%';
View Code
  • 查询表的数据条数、表名、中文表名
SELECT a.num_rows, a.table_name, b.comments
  FROM user_tables a, user_tab_comments b
 WHERE a.table_name = b.table_name
 ORDER BY table_name;
View Code
  • 查询索引
SELECT * FROM user_indexes
  • 清空表数据
truncate table tablename
  • 将一个表的内容复制到另一个表
create table  to_table as SELECT * FROM from_tablename WHERE columnname = ''  --存放至新表
insert into to_table select * from table2 where                               --存放至已有表
  • 使用变量:&+变量名称
SELECT * FROM tablename WHERE columnname = '&变量'
  • DECODE使用:DECODE是oracle提供的一种if then else简写函数,该函数也可以应用在批量更新上,一条语句实现一个字段修改成多个值
DECODE(value, if1, then1, if2,then2, if3,then3, . . . else )
  • merge使用:判断表中有没有符合on()条件中的数据,有了就更新数据,没有就插入数据。
merge into test_merge a
using test b
on(a.no=b.no)
when matched then update set a.no2=b.no2 where a.no<>1
when not matched then insert values(b.no,b.no2)  where a.no<>100
View Code

存储过程语法

  创建过程

  • 无参
create or replace procedure NoParPro  
 as  //声明  
 ;  
 begin // 执行  
 ;  
 exception//存储过程异常  
 ;  
 end;  
View Code
  • 带参
create or replace procedure queryempname(sfindno emp.empno%type)   //%type表示参数属性与表emp的empno字段一样
as  
   sName emp.ename%type;  
   sjob emp.job%type;  
begin  
       ....  
exception  
       ....  
end;
View Code
  • 带参并赋值
create or replace procedure runbyparmeters    
    (isal in emp.sal%type,   
     sname out varchar,  
     sjob in out varchar)  
 as   
    icount number;  
 begin  
      select count(*) into icount from emp where sal>isal and job=sjob;  
      if icount=1 then  
        ....  
      else  
       ....  
     end if;  
exception  
     when too_many_rows then  
     DBMS_OUTPUT.PUT_LINE('返回值多于1行');  
     when others then  
     DBMS_OUTPUT.PUT_LINE('在RUNBYPARMETERS过程中出错!');  
end;  
View Code

  存储过程调用

  • 过程调用方式一
DECLARE
  realsal  emp.sal%TYPE;
  realname VARCHAR(40);
  realjob  VARCHAR(40);
BEGIN  / / 过程调用开始
  realsal := 1100;
  realname := '';
  realjob := 'CLERK';
  runbyparmeters(realsal, realname, realjob);  --必须按顺序 / / runbyparmeters为存储过程 
  dbms_output.put_line(realname || '   ' ||realjob);
END;
/ / 过程调用结束
View Code
  • 过程调用方式二
declare  
     realsal emp.sal%type;  
     realname varchar(40);  
     realjob varchar(40);  
begin    //过程调用开始  
     realsal:=1100;  
     realname:='';  
     realjob:='CLERK';  
     --指定值对应变量顺序可变  
     runbyparmeters(sname=>realname,isal=>realsal,sjob=>realjob);      //runbyparmeters为存储过程
    DBMS_OUTPUT.PUT_LINE(REALNAME||'   '||REALJOB);  
END;  //过程调用结束  
View Code
  • 过程调用方式三(SQL命令行方式下)
 SQL>exec  proc_emp('参数1','参数2');//无返回值过程调用  
 SQL>var vsal number  
    SQL> exec proc_emp ('参数1',:vsal);// 有返回值过程调用  
      或者:call proc_emp ('参数1',:vsal);// 有返回值过程调用 
View Code

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!