两张表进行数据的拷贝,最常用的拷贝语句是:
insert into select 和 select into from
但是请绝对的注意:
在Oracle中select into from不可以使用-----原因很简单:select into是PL/SQL language 的赋值语句!如果使用则Oracle会抛出0RA-00905:missing keyword的异常!
但是可以用create table select代替该功能!!!具体参考下面测试代码!
但是在Sql Server中可以正常使用。
先做个小测试:
-- 建表
create table test1(
id number primary key,
testname varchar2(20),
createtime date,
falg varchar2(10)
);
create table test2(
id number primary key,
testname varchar2(20),
createtime date,
falg varchar2(10)
);
-- 插入测试数据
insert into test1 values(1,'测试数据1....1',sysdate-2,'N');
insert into test1 values(2,'测试数据1....2',sysdate-2,'N');
insert into test1 values(3,'测试数据1....3',sysdate-2,'N');
commit;
-- 使用insert into select 拷贝数据(注意红色部分,可以自动生成id序列值)
insert into test2(id,testname,createtime,falg)
select seq_test.nextval,t1.testname,t1.createtime,t1.falg from test1 t1;
-- 使用 create table select 创建被拷贝数据(注意要删除test2表先)
create table test2 as select t1.id,t1.testname,t1.createtime,t1.falg from test1 t1;
-- select into from 不可以,抛异常
select t1.id,t1.testname,t1.createtime,t1.falg into test2(id,testname,createtime,falg)
from test1 t1;
-- PL/SQL language 中select into赋值语句的测试使用
create or replace procedure test1_prod
is
aa varchar2(100);
begin
select t1.testname into aa from test1 t1 where id=1;
dbms_output.put_line('t1.testname= '|| aa);
end;
总结:
数据拷贝,建议使用insert into select;
使用insert into select时如果对拷贝表生成id序列值,需要在select中以查询出的形式从sequence中查询出,再插入拷贝表;比如:
insert into test2(id,testname,createtime,falg)
select seq_test.nextval,t1.testname,t1.createtime,t1.falg from test1 t1;
典型从test1表查询出数据插入test2表,test2要自动插入id,看上面代码,id要在select中先从sequence中查询出了!!
【转载】http://blog.sina.com.cn/s/blog_56d8ea9001011iqd.html
分享到:
相关推荐
ora-01033:oracle initialization or shutdown in progress 解决方法 ora-01033:oracle initialization or shutdown in progress 解决方法 ora-01033:oracle initialization or shutdown in progress 解决方法 ora-...
ORACLE INSERT INTO SELECT *FROM 分批提交例子
IMP-00009:异常结束导出文件解决方案.docx
oracle12c程序连接时异常: ORA-01017: 用户名/口令无效; 登录被拒绝 的解决方案。
Oracle中没有select into的用法! 在某些数据库中有select into的用法,用法是: select valueA,valueB into tableB from tableA; 上面这句语句的意思是将tableA表中的valueA和valueB字段的值和结构添加到tableB中,...
select into 和 insert into select 两种表复制语句 第一句(select into from)要求目标表(destTbl)不存在,因为在插入时会自动创建 第二句(insert into select from)要求目标表(destTbl)存在,由于目标表...
在运行查询SELECT * FROM V$SESSION 会出现ORA-29275:部分多字节字符的错误,这是什么原因开始我不得其解,网上也没有介绍什么好办法。本文给出答案。
ORA-32001:write to spfile requested but no spfile is in use请求写入spfile,但没有使用spfile的解决方法 在输入以下语句中报了这样的错误: SQL>alter system set control_files=’/u01/app/oracle/oradata/prod/...
ORA-01036:非法的变量名/编号 oracle特有的错误
oracle19c缺少的函数
oracle 12 c ORA-01017: 用户名/口令无效; 登录被拒绝
您可能感兴趣的文章:常用的Oracle doc命令(收藏)Oracle 多行记录合并/连接/聚合字符串的几种方法Oracle中字符串连接的实现方法php连接oracle数据库及查询数据的方法plsql连接oracle数据库报ora 12154错
oracle报错ora-12541:TNS无监听程序
NULL 博文链接:https://3w1h.iteye.com/blog/1211779
有关表死锁的详细图片 博文链接:https://meteor-1988.iteye.com/blog/1568695
最近在工作中遇到了一个问题,错误是Oracle dbca时报错:ORA-12547: TNS:lost contact,通过查找相关的资料终于找到了解决的方法,下面分享给大家,话不多说了,来一起看看详细的介绍吧。 环境: OS:RHEL6.5 x86-...
删除数据库实例时,出现不允许删除实例的解决方法
ORACLE权限关于with admin option和with grant option的用法,希望对大家有帮助! with admin option是用在系统权限上的,with grant option是用在对象权限上的。
解决ORACLE TNS: 无法解析指定的连接标识符; 里面有步骤