sqlserver如何将一张表中的一列的值更新到另外一张表

lanbao3308 2018-05-07 12:53:29
sqlserver如何将一张表中的一列的值更新到另外一张表


两张表结构完全一样,一个表名opcode,一个表名opcodebak
两表查询结果其中一个字段otype 的值有区别,通过两个表中的一列code关联,如何将A表中的otype值更新到B表中的otype



update opcode a set a.otype=b.otype from opcodebak b where a.code=b.code


update opcode set otype=a.otype from opcodebak as a where a.code=opcode.code;

update opcode set otype =select otype from opcodebak a where a.id=opcode.code;

写了几种语句都不对,求解
...全文
1106 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
lanbao3308 2018-05-07
  • 打赏
  • 举报
回复
不好意思弄错了,
二月十六 2018-05-07
  • 打赏
  • 举报
回复
引用 6 楼 lanbao3308 的回复:
[quote=引用 4 楼 sinat_28984567 的回复:]
update opcode   set otype=a.otype from opcodebak as a where a.code=opcode.code;
这句看着没问题,楼主说不对是怎么了?
我这样的提示[/quote] 表中code列有吗?字段名称是不是ocode啊?
二月十六 2018-05-07
  • 打赏
  • 举报
回复
--测试数据
if not object_id(N'opcode') is null
drop table opcode
Go
Create table opcode([code] int,[otype] int)
Insert opcode
select 1,1 union all
select 2,1 union all
select 3,1
GO
if not object_id(N'opcodebak') is null
drop table opcodebak
Go
Create table opcodebak([code] int,[otype] int)
Insert opcodebak
select 1,2 union all
select 2,3 union all
select 3,4
Go
--测试数据结束

update opcode set otype=a.otype from opcodebak as a where a.code=opcode.code;
SELECT * FROM opcode


二月十六 2018-05-07
  • 打赏
  • 举报
回复
update opcode   set otype=a.otype from opcodebak as a where a.code=opcode.code;
这句看着没问题,楼主说不对是怎么了?
lanbao3308 2018-05-07
  • 打赏
  • 举报
回复
引用 2 楼 wmxcn2000 的回复:
update a
set a.code = b.code
from a, b
where a.id = b.id




这样写好像不对呀


卖水果的net 2018-05-07
  • 打赏
  • 举报
回复
提示你的列名不对啊。 你把建表语句,也贴上来。
lanbao3308 2018-05-07
  • 打赏
  • 举报
回复
引用 4 楼 sinat_28984567 的回复:
update opcode   set otype=a.otype from opcodebak as a where a.code=opcode.code;


这句看着没问题,楼主说不对是怎么了?


我这样的提示
卖水果的net 2018-05-07
  • 打赏
  • 举报
回复
update a set a.code = b.code from a, b where a.id = b.id
lanbao3308 2018-05-07
  • 打赏
  • 举报
回复

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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