开发人员常用的Oracle技术

阅读更多

数据导出导入

参考:http://www.cnblogs.com/jason_lb/archive/2007/02/09/645586.html

 

导出数据exp

 

1)将数据库TEST完全导出,用户名system 密码manager 导出到D:\daochu.dmp中

exp system/manager@TEST file=d:\daochu.dmp full=y

 

2)将数据库中system用户与sys用户的表导出

exp system/manager@TEST file=d:\daochu.dmp owner=(system,sys)

 

3)将数据库中的表inner_notify、notify_staff_relat导出

exp aichannel/aichannel@TESTDB2 file= d:\datanewsmgnt.dmp tables=(inner_notify,notify_staff_relat) 

 

4)将数据库中的表table1中的字段filed1以"00"打头的数据导出

exp system/manager@TEST file=d:\daochu.dmp tables=(table1) query=" where filed1 like '00%'"

 

导入数据imp

 

1)将D:\daochu.dmp 中的数据导入 TEST数据库中。

imp system/manager@TEST  file=d:\daochu.dmp

imp aichannel/aichannel@TEST  full=y  file=d:\datanewsmgnt.dmp ignore=y

上面可能有点问题,因为有的表已经存在,然后它就报错,对该表就不进行导入。

在后面加上ignore=y 就可以了

 

2)将d:daochu.dmp中的表table1 导入

imp system/manager@TEST  file=d:\daochu.dmp  tables=(table1)

 

创建表空间

 

create tablespace TBLSMS
datafile 'D:/oracle/product/10.2.0/oradata/shenhq/tblsms.dbf' size 5m reuse 
AUTOEXTEND ON NEXT 10M MAXSIZE UNLIMITED
EXTENT MANAGEMENT LOCAL AUTOALLOCATE
/

 

 

与操作系统有一些相关,因为涉及数据文件的指定:

 

create tablespace TBLSMS
datafile '/opt/app/oracle/oradata/stariboss/tblsms.dbf' size 5m reuse 
AUTOEXTEND ON NEXT 10M MAXSIZE UNLIMITED

EXTENT MANAGEMENT LOCAL AUTOALLOCATE

/

 

 

创建用户并配置权限

 

 

-- Create the user
create user [user_name]
  identified by "[user_name]"
  default tablespace [default_tablespace_name]
  temporary tablespace [temp_tablespace_name]
  profile DEFAULT;
  
-- Grant/Revoke role privileges 
grant dba to [user_name];

-- Grant/Revoke system privileges 
grant insert any table to [user_name];
grant select any table to [user_name];
grant update any table to [user_name];
grant unlimited tablespace to [user_name];
 

 

 

 

典型的存储过程编写

不会写PL/SQL不叫会用Oracle

 

存储过程

spool .[log_name].log

prompt
prompt Creating procedure [procedure_name]
prompt
create or replace procedure [procedure_name]
  is
  sql_code varchar2(50);
  sql_errm varchar2(100);
  cursor [cur_name] is
    {select sql};

  v_cnt number;
begin
  v_cnt := 0;
  for v_instanceservice in [cur_name] loop
    begin
      {some update sql};
    
      v_cnt := v_cnt + 1;
    
      if mod(v_cnt, 10000) = 0 then
        commit;
      end if;
    exception
      when others then
        sql_code := sqlcode;
        sql_errm := sqlerrm;
        dbms_output.put_line(sql_code || ' ' || sql_errm ||
                             ' at servicestr ' ||
                             v_instanceservice.servicestr);
    end;
  end loop;
  commit;
  dbms_output.put_line('Total is:' || v_cnt);

end;
/


spool off

  函数

create or replace function [function_name]([parameters_name] [parameters_type])
  return varchar2 IS
  v_result         varchar2(100);
  {use parameters in sql as value}
begin
  v_result         := null;
  {process nothing to say}
  return v_result;
end [function_name];
 

 

修改表

增加字段

 

create or replace procedure ALTER_TABLE is
  v_count number;

begin
  select count(*)
    into v_count
    from user_tab_columns u
   where u.TABLE_NAME = [表名]
     and (u.COLUMN_NAME = [字段名,如果多个用OR拼接条件]);

  if v_count < 1 then
    execute immediate '[增加字段的ALTER脚本]';
   
  end if;
  commit;
end;
/
exec ALTER_TABLE;
DROP PROCEDURE ALTER_TABLE;

其他杂项

 

调整一批sequence的值

 

create or replace procedure adjust_seq_value_prc
 is
  sql_code    varchar2(50);
  sql_errm    varchar2(100);
  
  cursor cur_get_seq_info is
    select us.sequence_name, us.last_number, us.increment_by, us.cache_size
      from user_sequences us
     where us.sequence_name not in
           ('[sequence_name]', '[sequence_name]', ......);

begin
  for v_get_seq_info in cur_get_seq_info loop
    begin
      execute immediate 'alter sequence ' || v_get_seq_info.sequence_name ||
                        ' increment by [number_want_to_add] nocache';
    
      execute immediate 'select ' || v_get_seq_info.sequence_name ||
                        '.nextval from dual'
    
      execute immediate 'alter sequence ' || v_get_seq_info.sequence_name ||
                        ' increment by ' || v_get_seq_info.increment_by ||
                        ' nocache';
    exception
      when others then
        sql_code := sqlcode;
        sql_errm := sqlerrm;
        dbms_output.put_line(sql_code || ' ' || sql_errm);
    end;
  
  end loop;
end;
/
 

 

(有新知新得再补充,待续……)

 

 

你可能感兴趣的:(oracle,exp,imp,plsql)