A表有字段ID, Name,B表也有ID, Name,其中Name都是varchar2类型的,当A.Name<>B.Name时,update A.Name=B.Name,我的语句如下:
if (A.Name<>B.Name) then
update A.Name=B.Name where A.ID=B.ID;
可是当A.Name或B.Name为有一个为NULL时,并不会判断出两者不等,语句要怎么写?
解决方案 »
- 怎么循环添加数据?
- oracle导出数据报错问题
- 要访问oracle数据库的机器的oracle客户端怎么能方便的安装?
- create user语句在Procedure中编译通不过:(
- oracle 数据库 查询 建立索引后为什么速度反而慢了?急!
- 为什么同一个Oracle数据库用不同的用户登陆进去看到的数据是不一样的呢?
- sql语言的向oracle转化 在线等!!!
- 同时启动两个OracleService,如何知道登录的是那个Oracle Instance?
- 大家帮忙啊.<游标问题>在线等待.....我所有的分.很简单.
- 如何创建oracle数据库,使用的是odbc方法连接数据库。
- ORACLE是开源的么?
- select 嵌套的奇怪问题
SQL> create table a(id int,name nvarchar2(20));Table created.SQL> create table b(id int,name nvarchar2(20));Table created.
SQL> insert into a select 1,'a' from dual;1 row created.SQL> insert into a select 2,'b' from dual;1 row created.SQL> insert into a select 3,null from dual;1 row created.SQL> insert into b select 1,'A' from dual;1 row created.SQL> insert into b select 2,'B' from dual;1 row created.SQL> insert into b select 3,'C' from dual;1 row created.SQL> commit;Commit complete.SQL> select * from a; ID NAME
---------- --------------------
1 a
2 b
3SQL> select * from b; ID NAME
---------- --------------------
1 A
2 B
3 C
SQL> update a set a.name=(select name from b where nvl(a.name,0)<>nvl(b.name,0) and a.id=b.id);3 rows updated.SQL> select * from a; ID NAME
---------- --------------------
1 A
2 B
3 C
我试了下,写了个可以执行的,希望高手给个更简单的语句
update a set name = (select b.name from a,b where nvl(a.name,0)<>nvl(b.name,0) and a.id=b.id)
where id in (select a.id from a,b where nvl(a.name,0)<>nvl(b.name,0) and a.id=b.id)
如果出现多条纪录不一样..就不行了..望高手指点.!
3楼写的那语句执行一次,a和b表数据一样的话,a表的数据就为空了!
但是,再执行一次a表就只剩下数据一样的那几条了,其他的都为空!
..........
update a set a.name=(select name from b where nvl(a.name,0)<>nvl(b.name,0) and a.id=b.id)
where id in (select id from b where nvl(a.name,0) <>nvl(b.name,0) and a.id=b.id)