SQL简单问题,在线给分。

hzyzx 2002-04-24 09:23:54
我有两个表TABLE1,TABLE2 两个表中都有NUM,NAME字段
现在我需要把TABLE1中的NAME字段数据更新
条件是字段NUM中的数据在TABLE2中的NUM能找到,如果找到就用TABLE2的NAME字段的数据
更换TABLE1的NAME字段数据,找不到就保留原来的。请问该怎么写?
...全文
46 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ghxghx 2002-04-24
  • 打赏
  • 举报
回复
楼上 kaikaihe(开开)的正确
高手啊!!!
我等低手真是自叹不如
hzyzx 2002-04-24
  • 打赏
  • 举报
回复
谢谢大家

我在ACCESS试过了,好象都不行,望指点!
ghxghx 2002-04-24
  • 打赏
  • 举报
回复
if exists (SELECT table2.[name] from table2 ,table1
where table1.num=table2.num)
begin
declare @aa varchar(20)
declare aa cursor for
SELECT table2.[name] from table2 ,table1 where table1.num=table2.num
open aa
fetch next from aa into @aa
while @@fetch_status=0
begin
update table1
set [name]=@aa
where current of aa
fetch next from aa into @aa
end
close aa
deallocate aa
end
ping2000 2002-04-24
  • 打赏
  • 举报
回复
update table1 from table2 set name=talbe2.name
where num=table2.num
billjjg 2002-04-24
  • 打赏
  • 举报
回复
update table1
set table1.name=(select table2.name from table2 where table2.num=num)
where table1.num in (select num from table2)
billjjg 2002-04-24
  • 打赏
  • 举报
回复
Please try:

update table1
set name=(select table2.name from table2,table1 where table2.num=table1.num)
where num in (select num from table2.num)

kaikaihe 2002-04-24
  • 打赏
  • 举报
回复
update table1 set name = table2.name from table2 where table1.num = table2.num
liu800323 2002-04-24
  • 打赏
  • 举报
回复
update tabel1 set name=table2.Name where table1.num=table2.num
你试一下吧

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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