0

我有一个sql更新查询如下:

update #tree c 
set treetop=p.treetop,
treeptag=p.treeptag,
adjust= 'a2a'
from #tree p ,#regions r 
where c.treeptag=''
and   c.xsreg=r.Region
and    c.xsreg <> c.reg
and c.tradedate=p.tradedate
and p.treeaotag=replace(r.srvid+':'+c.tradedate+':'+c.litag,'  ','')

我正在尝试在不使用“fromlist”的情况下使用连接编写查询,如下所示:

update #tree c 
set treetop=(select p.treetop from #tree p ,#regions r 
                      where c.treeptag=''
                      and   c.xsreg=r.Region
                      and    c.xsreg <> c.reg
                      and c.tradedate=p.tradedate
              and p.treeaotag=replace(r.srvid+':'+c.tradedate+':'+c.litag,'  ','')),
treeptag=(select p.treeptag from #tree p ,#regions r 
                      where c.treeptag=''
                      and   c.xsreg=r.Region
                      and    c.xsreg <> c.reg
                      and c.tradedate=p.tradedate
              and p.treeaotag=replace(r.srvid+':'+c.tradedate+':'+c.litag,'  ','')),
adjust= 'a2a'
where exists (select 1 from #tree p ,#regions r  where c.treeptag=''
and   c.xsreg=r.Region
and   c.xsreg <> c.reg
and c.tradedate=p.tradedate
and p.treeaotag=replace(r.srvid+':'+c.tradedate+':'+c.litag,'  ',''))

然而,上面的查询似乎返回了不正确的行数;任何建议都是最有帮助的。

4

1 回答 1

0

翻译是正确的,所以行数应该完全一样。如果表很小,请使用 DDL 和示例数据插入更新问题。否则,这些返回什么?

select count(1)
from #tree c, #tree p ,#regions r 
where c.treeptag=''
and   c.xsreg=r.Region
and    c.xsreg <> c.reg
and c.tradedate=p.tradedate
and p.treeaotag=replace(r.srvid+':'+c.tradedate+':'+c.litag,'  ','')

select count(disctint c.id)  -- or whatever the unique key on #tree is
from #tree c, #tree p ,#regions r 
where c.treeptag=''
and   c.xsreg=r.Region
and    c.xsreg <> c.reg
and c.tradedate=p.tradedate
and p.treeaotag=replace(r.srvid+':'+c.tradedate+':'+c.litag,'  ','')

以及这两个计数与更新相比如何?我原以为它会匹配第二个计数。

对于它的价值,它是 2012 年,所以使用正确的 ANSI 连接:

update c 
   set treetop=p.treetop, treeptag=p.treeptag, adjust='a2a'
  from #tree c
  join #tree p on c.tradedate=p.tradedate
  join #regions r on c.xsreg=r.Region 
 where c.treeptag='' and c.xsreg <> c.reg
   and p.treeaotag=replace(r.srvid+':'+c.tradedate+':'+c.litag,'  ','')

而且您的某些条件是直接反对的c,那么为什么将它们包含在子查询中呢?

update #tree c
   set treetop =(select p.treetop
                   from #tree p
                   join #regions r on c.xsreg=r.Region
                  where c.tradedate=p.tradedate
                    and p.treeaotag=replace(r.srvid+':'+c.tradedate+':'+c.litag,'  ','')),
       treeptag=(select p.treeptag
                   from #tree p
                   join #regions r on c.xsreg=r.Region 
                  where c.tradedate=p.tradedate
                    and p.treeaotag=replace(r.srvid+':'+c.tradedate+':'+c.litag,'  ','')),
       adjust  ='a2a'
 where c.treeptag='' and c.xsreg <> c.reg and -- << these two
exists (select 1
          from #tree p
          join #regions r on c.xsreg=r.Region
         where c.tradedate=p.tradedate
           and p.treeaotag=replace(r.srvid+':'+c.tradedate+':'+c.litag,'  ',''))
于 2012-10-07T08:43:55.020 回答