sqlserver update inner join 问题 ! 解决马上给分

给力地板油 2012-03-30 12:06:47
sql:代码

UPDATE RollingStock set quantity =a.quantity

from (select quantity,contractuntil,lastchange,addressno,partnumber from RollingStock where rollingstock_id>894) as a

inner join (select quantity,contractuntil,lastchange,addressno,partnumber from RollingStock where rollingstock_id<=894) as c

on a.addressno+a.partnumber=c.addressno+c.partnumber

where RollingStock.rollingstock_id<=894

and RollingStock.addressno+RollingStock.partnumber in (select d.addressno+d.partnumber

from (select quantity,contractuntil,lastchange,addressno,partnumber from RollingStock where rollingstock_id>894) as d

inner join (select quantity,contractuntil,lastchange,addressno,partnumber from RollingStock where rollingstock_id<=894) as e

on d.addressno+d.partnumber=e.addressno+e.partnumber )


update级联更新

sql的意思 更新RollingStock表的quantity 字段

将RollingStock表rollingstock_id>894 作为一个表 和 RollingStock表rollingstock_id<=894 作为一个表

做内联 如果有重复的数据 用RollingStock表rollingstock_id>894 的quantity 字段更新

RollingStock表rollingstock_id<=894 的quantity 字段

现在的SQL没有语法错误 也能更新 但更新的数据都是同样的结果 也就是说都是更新的一条死的数据 没有实现级联的更新

我在网上查过该语法 也没发现有什么不对的 请教高手了 给指点下吧
...全文
485 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
叶子 2012-03-30
  • 打赏
  • 举报
回复
跨表更新,我给楼主举个例子:

create table m_name (id int,name varchar(4))
insert into m_name
select 1,'张三' union all
select 2,'李四' union all
select 3,'王五'

select * from m_name
/*
id name
----------- ----
1 张三
2 李四
3 王五
*/

create table m_chengji (name varchar(4),kemu int,chengji int,id sql_variant)
insert into m_chengji
select '张三',1,95,null union all
select '张三',2,92,null union all
select '张三',3,91,null union all
select '李四',1,56,null union all
select '李四',2,76,null union all
select '李四',3,99,null union all
select '王五',1,57,null union all
select '王五',2,100,null union all
select '王五',3,67,null

select * from m_chengji
/*
name kemu chengji id
---- ----------- ----------- -----------
张三 1 95 NULL
张三 2 92 NULL
张三 3 91 NULL
李四 1 56 NULL
李四 2 76 NULL
李四 3 99 NULL
王五 1 57 NULL
王五 2 100 NULL
王五 3 67 NULL
*/

-- 更新m_chengji表的id为m_name中的id
update m_chengji
set id = a.id from m_chengji b left join m_name a on a.[name]=b.[name]
dawugui 2012-03-30
  • 打赏
  • 举报
回复

建议你提供详细的资料:
例如表的结构,表之间的关系,测试数据,相关算法及需要的结果。
这样有助于我们理解你的意思,更主要的是能尽快让你获得答案或解决问题的方法。

给力地板油 2012-03-30
  • 打赏
  • 举报
回复
是不是实体表的级联更新 和 查询出来的表 级联更新的结果会不同呢

写法肯定不一样 我试过
类似

UPDATE RollingStock set quantity =a.quantity

from (select quantity,contractuntil,lastchange,addressno,partnumber from RollingStock where rollingstock_id>894) as a

inner join (select quantity,contractuntil,lastchange,addressno,partnumber from RollingStock where rollingstock_id<=894) as c

on a.addressno+a.partnumber=c.addressno+c.partnumber


级联更新不起作用 全部更新结果

而加上 where RollingStock.rollingstock_id<=894

会更新rollingstock_id<=894 的结果

但如果你单独查询

select a.quantity,c.quantity,a.contractuntil,a.lastchange,a.searchname from

(select rollingstock_id,quantity,contractuntil,lastchange,searchname,partnumber from RollingStock where rollingstock_id>894) as a inner join

(select rollingstock_id,quantity,contractuntil,lastchange,searchname,partnumber from RollingStock where rollingstock_id<=894) as c

on a.searchname+a.partnumber=c.searchname+c.partnumber

是没有问题的 我猜实体表的更新 和查询后的表更新 在写法上肯定不一样

但是我的写法貌似已经找不到问题了 但结果不对 所以很困惑
gogodiy 2012-03-30
  • 打赏
  • 举报
回复
楼主先使用select看下是否能查询出你需要的数据。

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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