我有两张表t1(a,b,c),t2(d,e,f),结构一样,t1表中的c字段不能为null,现需要根据t2表的内容在update t1中的c字段,我是这样写的:update t1 a set a.c=(select b.f from t2 b where a.a=b.d and a.b=b.e and a.c=b.f),但在执行过程中,会提示“值不
能为null”,就是在t2表中可能没有符合条件的记录。我想判断一下,如果有数据就更新,没有就保持t1表中的c字段值不变,请教大家
这个sql改怎么写啊,谢谢大家!
能为null”,就是在t2表中可能没有符合条件的记录。我想判断一下,如果有数据就更新,没有就保持t1表中的c字段值不变,请教大家
这个sql改怎么写啊,谢谢大家!
解决方案 »
- Oracle sql用时间段判断问题~~续
- 现在需要做一个oracle列表的查询
- ADOX的问题!
- sql如何查询工资相同的客户名称@@@@@
- 请问oracle表中的字段有“自动编号”这个数据类型吗?
- 一个复杂的问题啊!!!一定有分
- 我也寻一条SQL语句,想了两天了,都没搞定! 关于一条语句中查同一字段的多个Count()!
- 回复必给分 ,在Oracle中如何查看连接过或正在连接数据库的用户电脑ip , hostname 以及用何种软件登录的,是以哪个用户名登入的??
- 各位高手,我这条SQL语句有问题,请帮帮忙!快!
- 我怎么通过oms将没有启动oracle intelligent agent的数据库节点上的数据到出来呢?
- 关于oracle的update的问题,望回复
- 有谁知道forall和merge的不同之处吗?
set a.c=(select b.f from t2 b where a.a=b.d and a.b=b.e and a.c=b.f and b.f is not null)
set a.c=(select b.f from t2 b where a.a=b.d and a.b=b.e and a.c=b.f and b.f is not null
set a.c=(select b.f from t2 b where a.a=b.d and a.b=b.e and a.c=b.f and b.f is not null
--补充,应该加上where条件限制,否则所有的记录都被更新了
where exists(select 1 from t2 b where a.a=b.d and a.b=b.e and a.c=b.f );
set a.c=(select b.f from t2 b where a.a=b.d and a.b=b.e and a.c=b.f)
where exists (select 1 from t2 b where a.a=b.d and a.b=b.e and a.c=b.f)
关键在exists语句。
set a.c=(select b.f from t2 b where a.a=b.d and a.b=b.e and a.c=b.f and b.f is not null
where exists(select 1 from t2 b where a.a=b.d and a.b=b.e and a.c=b.f and b.f is not null);
update t1 a set a.c=(select b.f from t2 b where a.a=b.d and a.b=b.e and a.c=b.f)
where exists(select 1 from t2 b where a.a=b.d and a.b=b.e and a.c=b.f)
使用了“a.c=b.f”,再用“b.f is not null”是多此一举。
哈哈,有意思,大家都被楼主迷糊了,他可能要的是这样的效果(如果他的b.f是可空的话):
update t1 a
set a.c=(select b.f from t2 b where a.a=b.d and a.b=b.e and b.f is not null)
where exists(select 1 from t2 b where a.a=b.d and a.b=b.e and b.f is not null);
update t1 a
set a.c = NVL((select b.f from t2 b where a.a=b.d and a.b=b.e and a.c=b.f), a.c);