错误SQL语法来自不同服务器的多个表

时间:2016-01-07 05:17:31

标签: sql sql-server

sql语法如下:

select 
    a.secCode, b.[Haircut (%)], c.[Absolute IDR (Miliar)] 
from 
    openquery(PDC_MYAPPS, 'select secCode from mii.secReq') a
left join 
    (select * from RC_Saham) b on a.secCode = b.kode 
left join 
    (select * from Absolute_IDR) c on b.kode = c.[Kode Saham]
where 
    b.[Haircut (%)] is not null 

union

select * 
from openquery(MANTARAY, 'select b.code_ic, a.PERC_HRCT 
                          from cminstruments a, instrument_codes b
                          where a.ID_CMI_CAPCO = b.INS_ID_INS_CAPCO')
where  
    code_Ic in (select a.secCode 
                from openquery(PDC_MYAPPS, 'select secCode 
                                            from mii.secReq') a 
                left join  
                   (select * from RC_Saham) b on a.secCode = b.kode 
                left join 
                   (select * from Absolute_IDR) c on b.kode = c.[Kode Saham]
                where b.[Haircut (%)] is null) 

我收到此错误:

  

Msg 205,Level 16,State 1,Line 2
  使用UNION,INTERSECT或EXCEPT运算符组合的所有查询在目标列表中必须具有相同数量的表达式。

任何想法如何解决?谢谢

1 个答案:

答案 0 :(得分:0)

错误消息本身说明,目标列表中的表达式数量相等。

尝试这样的事情,

SELECT a.secCode
      ,b.[Haircut (%)]
      ,c.[Absolute IDR (Miliar)]
FROM   OPENQUERY(PDC_MYAPPS ,'select secCode from mii.secReq')a
       LEFT JOIN (
                SELECT *
                FROM   RC_Saham
            )b
            ON  a.secCode = b.kode
       LEFT JOIN (
                SELECT *
                FROM   Absolute_IDR
            )c
            ON  b.kode = c.[Kode Saham]
WHERE  b.[Haircut (%)] IS NOT NULL 
UNION
SELECT a.secCode
      ,b.[Haircut (%)]
      ,c.[Absolute IDR (Miliar)]
FROM   OPENQUERY(
           MANTARAY
          ,
           'select b.code_ic, a.PERC_HRCT from cminstruments a, instrument_codes b
    where a.ID_CMI_CAPCO = b.INS_ID_INS_CAPCO'
       )
WHERE  code_Ic IN (SELECT a.secCode
                   FROM   OPENQUERY(PDC_MYAPPS ,'select secCode from mii.secReq')
                          a
                          LEFT JOIN (
                                   SELECT *
                                   FROM   RC_Saham
                               )b
                               ON  a.secCode = b.kode
                          LEFT JOIN (
                                   SELECT *
                                   FROM   Absolute_IDR
                               )c
                               ON  b.kode = c.[Kode Saham]
                   WHERE  b.[Haircut (%)] IS NULL)