一个简单SQL的UPDATE问题

gt_sql 2012-07-24 09:57:36

with a as

(select distinct charge_code,supplyer
from yp_in_detl
where supplyer is not null
and supplyer <>'111111'
and supplyer <>''
and charge_code not exists
('015217','014747','015325','015360',
'015318','015215','015178','015098','015215','015178',
'015199','015106','015092','015082','015004','015028','015049','015018',
'014996','014926','014888','014859','014877','014846','014840','014834',
'014836','014827','014829','014830','014821','014813','014800','014776',
'014797','014758','014744','014731','014681','014672','014650','007084',
'014643','014631','014634','013828','013477','013620','014409','013064',
'007605','007103','007551','007382','005839','007068','014654','014746'))

update yp_dict set trad_mark=a.supplyer where charge_code=a.charge_code

错误提示:无法绑定由多个部分组成的标识符 "a.charge_code"。

改成下面的语句可以吗?
with a as

(select distinct charge_code,supplyer
from yp_in_detl
where supplyer is not null
and supplyer <>'111111'
and supplyer <>''
and charge_code not exists
('015217','014747','015325','015360',
'015318','015215','015178','015098','015215','015178',
'015199','015106','015092','015082','015004','015028','015049','015018',
'014996','014926','014888','014859','014877','014846','014840','014834',
'014836','014827','014829','014830','014821','014813','014800','014776',
'014797','014758','014744','014731','014681','014672','014650','007084',
'014643','014631','014634','013828','013477','013620','014409','013064',
'007605','007103','007551','007382','005839','007068','014654','014746'))

update yp_dict set trad_mark=(select supplyer from a) where charge_code=(select charge_code from a)

另外,这个语句可以怎么优化修改下

...全文
79 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
gt_sql 2012-07-24
  • 打赏
  • 举报
回复
x谢谢
  • 打赏
  • 举报
回复

with a as

(select distinct charge_code,supplyer
from yp_in_detl
where supplyer is not null
and supplyer <>'111111'
and supplyer <>''
and charge_code not in
('015217','014747','015325','015360',
'015318','015215','015178','015098','015215','015178',
'015199','015106','015092','015082','015004','015028','015049','015018',
'014996','014926','014888','014859','014877','014846','014840','014834',
'014836','014827','014829','014830','014821','014813','014800','014776',
'014797','014758','014744','014731','014681','014672','014650','007084',
'014643','014631','014634','013828','013477','013620','014409','013064',
'007605','007103','007551','007382','005839','007068','014654','014746'))


--这样应该可以。像油画建议你提供原始数据和期待结果
gt_sql 2012-07-24
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
SQL code

update update b set trad_mark=a.supplyer inner join yp_dict b on b.charge_code=a.charge_code
[/Quote]
还能不能把with a as 部分查询优化下
  • 打赏
  • 举报
回复

with a as

(select distinct charge_code,supplyer
from yp_in_detl
where supplyer is not null
and supplyer <>'111111'
and supplyer <>''
and charge_code not in
('015217','014747','015325','015360',
'015318','015215','015178','015098','015215','015178',
'015199','015106','015092','015082','015004','015028','015049','015018',
'014996','014926','014888','014859','014877','014846','014840','014834',
'014836','014827','014829','014830','014821','014813','014800','014776',
'014797','014758','014744','014731','014681','014672','014650','007084',
'014643','014631','014634','013828','013477','013620','014409','013064',
'007605','007103','007551','007382','005839','007068','014654','014746'))

  • 打赏
  • 举报
回复

update update b set trad_mark=a.supplyer inner join yp_dict b on b.charge_code=a.charge_code
gt_sql 2012-07-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
SQL code
update b set trad_mark=a.supplyer from yp_dict b,a where charge_code=a.charge_code
[/Quote]

犯了一个这个低级的错误

还能不能把with a as 部分查询优化下
--小F-- 2012-07-24
  • 打赏
  • 举报
回复
update b  set trad_mark=a.supplyer from yp_dict b,a where charge_code=a.charge_code

34,587

社区成员

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

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