Decode360's Blog

业精于勤而荒于嬉 QQ:150355677 MSN:decode360@hotmail.com

  BlogJava :: 首页 :: 新随笔 :: 联系 ::  :: 管理 ::
  302 随笔 :: 26 文章 :: 82 评论 :: 0 Trackbacks
    最近看同事操作数据库,用到了copy命令,惊叹自己之前竟然不知道。赶紧看了一下,虽然这么命令很简单,但是确实是很有用。
 
 
    首先看一下文档里的语法定义:
COPY {FROM database | TO database | FROM database TO database}{APPEND|CREATE|INSERT|REPLACE} destination_table [(column, column, column, ...)]USING query
 
    下面对语法逐一说明:
 
1、DATABASE
 
    database 对应以下的语法格式是:username[/password]@connect_identifier
 
    注意:如果不加密码,则sqlplus会提示你输入密码。考虑到安全性,则应该不写密码。
 
2、FROM|TO子句
 
    可以注意到,在这个语法里,FROM 和 TO 两个子句都是可以单独出现的(当然一起出现是最常见的情况)。当没有指定 FROM/TO 子句,则使用当前连接的数据库。
 
    无论是 FROM 还是 TO 子句中连接的数据库,都不能是 SYSDBA 或 SYSOPER 权限的用户。
 
3、COPY类型
 
    APPEND
    --表存在则INSERT,不存在则CREATE
    Inserts the rows from query into destination_table if the table exists. If destination_table does not exist, COPY creates it.

    CREATE
    --新建一个表,如果表存在则出错
    Inserts the rows from query into destination_table after first creating the table. If destination_table already exists, COPY returns an error.
    INSERT
    --向表中插入记录
    Inserts the rows from query into destination_table. If destination_table does not exist, COPY returns an error. When using INSERT, the USING query must select one column for each column in destination_table.
    REPLACE
    --无论表是否存在,均清空然后CREATE
    Replaces destination_table and its contents with the rows from query. If destination_table does not exist, COPY creates it. Otherwise, COPY drops the existing table and replaces it with a table containing the copied data.
 
4、destination_table
 
    注意指定目标表的同时,是可以指定其字段的,但是其字段数目和类型(如果已经存在)必须与后面的query中相一致,否则报错。
 
------------------------------------
 
    下面举例说明:
 
SQL> COPY FROM HR/HR @HQ TO JOHN/kkk @WEST REPLACE WESTEMPLOYEES USING SELECT * FROM EMPLOYEES;
 
    别的没什么可说的,但是要注意如果使用新建表的时候,新建的表的字段属性,都是与原表相同的。在一般的情况下这都是没有什么问题的,但是会有一种比较特殊的情况,就是例如:原始库的字符集为ZHS16GBK,而目标库的字符集为UTF-8,因为UTF-8中一个汉字占3个字节,而ZHS16GBK是2个。所以当原始库中的一个属性为varchar2(2)且为一个汉字的字段,就无法导入到UTF-8库中了,会报错:ORA-12899: value too large for column ...
 
    遇到这种情况,建议修改原始库的字段属性,如果没有权限,则可以在目标库中手工建立一下表,将字段属性调整到合适,然后使用INSERT或者APPEND类型COPY。
 
    另注:select * from nls_database_parameters; 查看字符集
 
 
 




-The End-

posted on 2009-05-23 23:10 decode360-3 阅读(840) 评论(0)  编辑  收藏 所属分类: Oracle

只有注册用户登录后才能发表评论。


网站导航: