如何将SQL连接语法应用于旧连接

时间:2012-06-15 09:14:03

标签: sql sql-server sql-server-2008 stored-procedures

当我将遗留连接语法重构为ANSI连接语法时,我遇到了问题,因为我们数据库中的大多数过程都使用了旧连接。这是我需要更改它以使用SQL'JOIN'语法而不是普通的旧连接的代码。任何人都可以建议我如何做到这一点?

    select 
          a.userkey, 
          a.username,
          c.monthd ,
          b.currencykey 
     from 
         #users a,
         #invoicedata b,
          #revdate c
     where 
         (a.userkey >= b.userkey or a.userkey <= b.userkey) 
          and b.idate between c.startdate and c.enddate
      group by 
           a.userkey,
           a.username,
           c.monthd,
           b.currencykey

       order by c.id,a.username,b.currencykey

1 个答案:

答案 0 :(得分:3)

此部分(a.userkey >= b.userkey or a.userkey <= b.userkey)似乎不正确,您不能order by列(至少没有agregate个功能)不在group by。< / p>

所以,可能是这样的:

select 
      a.userkey, 
      a.username,
      c.monthd ,
      b.currencykey 
 from #users a
 inner join #invoicedata b on a.userkey = b.userkey
 inner join #revdate c on b.idate between c.startdate and c.enddate
 group by 
      a.userkey,
      a.username,
      c.monthd,
      b.currencykey

我还用内连接替换了嵌套连接,但这不是必需的:)


另一方面,您可能需要交叉加入:

select 
      a.userkey, 
      a.username,
      c.monthd ,
      b.currencykey 
 from #users a
 cross join #invoicedata b
 inner join #revdate c on b.idate between c.startdate and c.enddate
 group by 
      a.userkey,
      a.username,
      c.monthd,
      b.currencykey