需求:

表a(com_name,stock_code,com_sacle,mark,market_location,company_name)

表b(com_name,stock_code,com_sacle)

如果a.stock_code=b.stock_code 把b.com_name,b.com_scale 插入a.com_name,a.com_scale

如果表b.stock_code 在表a中没有 则把表b(com_name,stock_code,com_sacle)插入表a

过程:

--根据tmp_stock表的字段补全stock_collection的字段
update (select a.com_name a1,a.com_scale a2,b.com_name b1,b.com_scale b2
from stock_collection a,tmp_stock b where a.stock_code=b.stock_code)
set a1=b1,a2=b2;

-----------------出现错误-----------------------

错误报告 -
SQL 错误: ORA-01779: 无法修改与非键值保存表对应的列
01779. 00000 - "cannot modify a column which maps to a non key-preserved table"
*Cause: An attempt was made to insert or update columns of a join view which
map to a non-key-preserved table.
*Action: Modify the underlying base tables directly.

------解决办法:设置唯一约束

alter table tmp_stock modify stock_code unique;

----------------------------------------------------

--插入tmp_stock表中有,而stock_collection表中没有的数据
insert into stock_collection(stock_code,com_name,com_scale)
select stock_code,com_name,com_scale from tmp_stock b 
where b.stock_code not in (select stock_code from stock_collection a);

Oracle-两表关联更新和插入的更多相关文章

  1. ORACLE 两表关联更新三种方式

    不多说了,我们来做实验吧. 创建如下表数据 select * from t1 ; select * from t2; 现需求:参照T2表,修改T1表,修改条件为两表的fname列内容一致. 方式1,u ...

  2. oracle 两表关联查询

      oracle 两表关联查询 CreationTime--2018年7月4日17点27分 Author:Marydon 情景描述 查询学生表student,sname,sex,age信息及所在班级c ...

  3. Oracle两表关联,只取B表的第一条记录

    背景:  A表.B表两表关联,关联出来的结果里B表有不止一条,需求是只要B表结果中的某一条(按某字段排序) 首先想到了直接写个带排序的子查询去匹配外围的值,从这个结果集中只要第一条,但是经过验证发现, ...

  4. 两表关联更新数据——oracle

    from testb b where b.id=a.id) ; (where exists(select 1 from testb b where b.id=a.id):如果没有这个条件,不匹配的选项 ...

  5. Oracle 多表关联更新

    drop table course; create table course ( id integer, teacherNo integer, teacherDesc ), teacherName ) ...

  6. postgresql 两表关联更新

    UPDATE 要更新的表 SET 字段1 = cqt.字段1, 字段2 = cqt.字段2, FROM 数据来源表 cqt WHERE 要更新的表.bsm = cqt.bsm

  7. 两表关联更新,用于update 回滚

    create table test1 as select * from dba_objects; create table test2 as select * from dba_objects; cr ...

  8. Oracle\MS SQL Server Update多表关联更新

    原文:Oracle\MS SQL Server Update多表关联更新 一条Update更新语句是不能更新多张表的,除非使用触发器隐含更新.而表的更新操作中,在很多情况下需要在表达式中引用要更新的表 ...

  9. Oracle中如何实现Mysql的两表关联update操作

    在看<MySQL 5.1参考手册>的时候,发现MySQL提供了一种两表关联update操作.原文如下: UPDATE items,month SET items.price=month.p ...

随机推荐

  1. 【Leetcode】【Medium】Subsets II

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  2. msvcr100.dll问题描述及修复方式

    出现问题的大部分原因是因该文件被木马病毒破坏导致系统找不到此文件,出现错误提示框,想要解决此问题只需找到专业的DLL文件下载网站,下载该文件,复制到相应目录.即可解决.msvcr100.dll为Vis ...

  3. MYSQL 升序排序但值为0的排最后

    转载 http://blog.csdn.net/looksun/article/details/51445205 如一张表的数据如下: 需要根据gz列的值进行升序排序,但值为0的排在最后面,即最终结果 ...

  4. Spark Executor内幕彻底解密:Executor工作原理图、ExecutorBackend注册源码解密、Executor实例化内幕、Executor具体工作内幕

    本课主题 Spark Executor 工作原理图 ExecutorBackend 注册源码鉴赏和 Executor 实例化内幕 Executor 具体是如何工作的 Spark Executor 工作 ...

  5. 如何使用jMeter对某个OData服务进行高并发性能测试

    For project reason I have to measure the performance of OData service being accessed parallelly. And ...

  6. 20140316 曼联VS利物浦,罗杰斯的小九九,当4312对上4231

    一.预测的阵式!? 赛前看fourfourtwo,Whoscored的首发预测,楼主立刻意淫了阵式图,并且大言不惭在微博贴了当晚的热战区域(上图的左侧),并疑惑着,格伦·囧森打左后卫,那这……显然是逼 ...

  7. Perl 修改文件内容

    把test.txt文件中的字符aaa替换成bbb perl -pi -e "s/aaa/bbb/gi" test.txt 把test.txt文件中的字符aaa替换成bbb,并生成一 ...

  8. Gluon 实现 dropout 丢弃法

    多层感知机中: hi 以 p 的概率被丢弃,以 1-p 的概率被拉伸,除以  1 - p import mxnet as mx import sys import os import time imp ...

  9. PHP使用in_array函数检查数组中是否存在某个值

    PHP使用 in_array() 函数检查数组中是否存在某个值,如果存在则返回 TRUE ,否则返回 FALSE. bool in_array( mixed needle, array array [ ...

  10. linq 和lamba表达式

    一.什么是Linq(what)二.Linq的优点(why)三.Linq查询的步骤(how)四.查询基本操作五.結合實例代碼(具體聯繫用linqtosql來寫的增刪改查)一.什么是Linq(what). ...