sql update 关联修改

暮上年华依旧 2013-09-04 02:39:15
以下语句是否有语法错误?
update a set a.funitid=b.fbaseunit
from T_BD_MaterialInventory a,T_BD_Material b
where b.fid=a.fmaterialid and b.fbaseunit <> a.funitid
...全文
470 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
请叫我-雷人 2013-09-04
  • 打赏
  • 举报
回复
update T_BD_MaterialInventory a set a.funitid=(select distinct b.fbaseunit from T_BD_Material b where b.fid=a.fmaterialid and b.fbaseunit <> a.funitid ) 试试。
异常异长 2013-09-04
  • 打赏
  • 举报
回复
update T_BD_MaterialInventory a 
   set a.funitid=(select b.fbaseunit 
                    from T_BD_Material b 
                   where  b.fid=a.fmaterialid 
                     and b.fbaseunit <> a.funitid 
                     and rownum = 1)
这个应该是可以的
  • 打赏
  • 举报
回复
引用 9 楼 yu_java_session 的回复:
[quote=引用 7 楼 HJ_daxian 的回复:] a.fmaterialid 和 b.fid一对多 还是一对一 如果一对一 1L应该可以 如果一对多 那可以加个条件 update T_BD_MaterialInventory a set a.funitid=(select fbaseunit from T_BD_Material b where a.fmaterialid = b.fid and a.funitid <> b.fbaseunit and rownum = 1)
确实是一对多的情况,那这个SQL脚本的意思是不是说:将T_BD_MaterialInventory 表中的 funitid 全部改成select子句中查出来的那个值?[/quote] 加个rownum = 1 就在查询出在对应表T_BD_Material中多行fbaseunit中的第一行数据 如果子查询返回多行数据 就会报错 单行查询返回多个值 必须只能返回一行
  • 打赏
  • 举报
回复
引用 8 楼 yu_java_session 的回复:
[quote=引用 6 楼 xxhhbb1538 的回复:] [quote=引用 1 楼 xxhhbb1538 的回复:] 语法错误

update T_BD_MaterialInventory a 
   set a.funitid=(select b.fbaseunit 
                    from T_BD_Material b 
                   where  b.fid=a.fmaterialid 
                     and b.fbaseunit <> a.funitid 
                     and rownum = 1)
[/quote]大哥 别走 决战到天亮啊 必须搞出来啊 不能打空炮弹[/quote] 仔细看看啊,我这个改过了哎呀,最后加了个rownum=1啊,你自己试试
暮上年华依旧 2013-09-04
  • 打赏
  • 举报
回复
引用 7 楼 HJ_daxian 的回复:
a.fmaterialid 和 b.fid一对多 还是一对一 如果一对一 1L应该可以 如果一对多 那可以加个条件 update T_BD_MaterialInventory a set a.funitid=(select fbaseunit from T_BD_Material b where a.fmaterialid = b.fid and a.funitid <> b.fbaseunit and rownum = 1)
确实是一对多的情况,那这个SQL脚本的意思是不是说:将T_BD_MaterialInventory 表中的 funitid 全部改成select子句中查出来的那个值?
暮上年华依旧 2013-09-04
  • 打赏
  • 举报
回复
引用 6 楼 xxhhbb1538 的回复:
[quote=引用 1 楼 xxhhbb1538 的回复:] 语法错误

update T_BD_MaterialInventory a 
   set a.funitid=(select b.fbaseunit 
                    from T_BD_Material b 
                   where  b.fid=a.fmaterialid 
                     and b.fbaseunit <> a.funitid 
                     and rownum = 1)
[/quote]大哥 别走 决战到天亮啊 必须搞出来啊 不能打空炮弹
  • 打赏
  • 举报
回复
a.fmaterialid 和 b.fid一对多 还是一对一 如果一对一 1L应该可以 如果一对多 那可以加个条件 update T_BD_MaterialInventory a set a.funitid=(select fbaseunit from T_BD_Material b where a.fmaterialid = b.fid and a.funitid <> b.fbaseunit and rownum = 1)
  • 打赏
  • 举报
回复
引用 1 楼 xxhhbb1538 的回复:
语法错误

update T_BD_MaterialInventory a 
   set a.funitid=(select b.fbaseunit 
                    from T_BD_Material b 
                   where  b.fid=a.fmaterialid 
                     and b.fbaseunit <> a.funitid 
                     and rownum = 1)
暮上年华依旧 2013-09-04
  • 打赏
  • 举报
回复
引用 1 楼 xxhhbb1538 的回复:
语法错误

update T_BD_MaterialInventory a 
   set a.funitid=(select b.fbaseunit 
                    from T_BD_Material b 
                   where  b.fid=a.fmaterialid 
                     and b.fbaseunit <> a.funitid )
还是不对
暮上年华依旧 2013-09-04
  • 打赏
  • 举报
回复
引用 2 楼 HJ_daxian 的回复:
直接update是不支持超过1个表关联的 只能通过子查询关联更新
求大哥指点,将上述语句改对,感谢!
暮上年华依旧 2013-09-04
  • 打赏
  • 举报
回复
引用 楼主 yu_java_session 的回复:
以下语句是否有语法错误? update a set a.funitid=b.fbaseunit from T_BD_MaterialInventory a,T_BD_Material b where b.fid=a.fmaterialid and b.fbaseunit <> a.funitid
还是不对
  • 打赏
  • 举报
回复
直接update是不支持超过1个表关联的 只能通过子查询关联更新
  • 打赏
  • 举报
回复
语法错误

update T_BD_MaterialInventory a 
   set a.funitid=(select b.fbaseunit 
                    from T_BD_Material b 
                   where  b.fid=a.fmaterialid 
                     and b.fbaseunit <> a.funitid )

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧