Excel查询 - 参数数量无效和描述符索引错误无效

时间:2016-08-01 14:54:39

标签: excel

我在公司的财务部门工作,并希望从我们的服务器(SQL)中提取数据,以便为董事会汇总报告。

所以我有几个excel文件,我使用excel查询来检索数据并进行演示。

我一直在升级我的查询并遇到这个障碍。

这段代码工作得很好:    选择

  INTERNAL_REFERENCE as ref,
  CMP_CODE AS CMP_CODE,
  COUNTERPARTY_CODE AS BANK_CODE,
  TRANSACTION_CODE AS TRANS_CODE,
  CAST(CONVERT(varchar(10), AMO_END_DATE, 110) AS datetime) AS DATE,
  SUM([AMORTIZATION]) AS AMOUNT
FROM
  [SAGE_MTC_FRP].[dbo].[LOAN_SCHEDULE] inner join [SAGE_MTC_FRP].[dbo].    [LOANS]
  on [SAGE_MTC_FRP].[dbo].[LOAN_SCHEDULE].LOAN_ID=[SAGE_MTC_FRP].[dbo].    [LOANS].LOAN_ID
WHERE
  (AMO_END_DATE>=?) AND (BOOK_DATE<?) AND
  (TRANSACTION_CODE<>'CPCA' AND TRANSACTION_CODE<>'CPCF' AND  TRANSACTION_CODE<>'RENT') AND 
  IS_DELETED=0 AND VERSION_NUMBER=1 AND CMP_CODE='MTG'
GROUP BY 
  INTERNAL_REFERENCE, CMP_CODE, COUNTERPARTY_CODE, TRANSACTION_CODE,     AMO_END_DATE

但是,当我将所有代码全部放入union时,它就会出现这些2x错误:

问题: 参数数量无效

无效的描述符索引

代码:

SELECT * FROM


(
SELECT 

  INTERNAL_REFERENCE as ref,
  CMP_CODE AS CMP_CODE,
  COUNTERPARTY_CODE AS BANK_CODE,
  TRANSACTION_CODE AS TRANS_CODE,
  CAST(CONVERT(varchar(10), AMO_END_DATE, 110) AS datetime) AS DATE,
  SUM([AMORTIZATION]) AS AMOUNT
FROM
  [SAGE_MTC_FRP].[dbo].[LOAN_SCHEDULE] inner join [SAGE_MTC_FRP].[dbo].    [LOANS]
  on [SAGE_MTC_FRP].[dbo].[LOAN_SCHEDULE].LOAN_ID=[SAGE_MTC_FRP].[dbo].    [LOANS].LOAN_ID
WHERE
  (AMO_END_DATE>=?) AND (BOOK_DATE<?) AND
  (TRANSACTION_CODE<>'CPCA' AND TRANSACTION_CODE<>'CPCF' AND  TRANSACTION_CODE<>'RENT') AND 
  IS_DELETED=0 AND VERSION_NUMBER=1 AND CMP_CODE='MTG'
GROUP BY 
  INTERNAL_REFERENCE, CMP_CODE, COUNTERPARTY_CODE, TRANSACTION_CODE,     AMO_END_DATE

UNION ALL

SELECT
    CL_CODE as ref
    ,LEFT([ACC_CODE] , 3) AS CMP_CODE
    ,[COUNTERPARTY_CODE] AS BANK_CODE
    ,RIGHT([CL_DESCRIPTION] , 3) AS TRANS_CODE
    ,CAST(CONVERT(varchar(10), [END_DATE], 110) AS datetime) AS DATE
    ,[CL_AMOUNT] AS AMOUNT
FROM 
[SAGE_MTC_FRP].[dbo].[CREDIT_LINES]
WHERE 
(END_DATE>?) AND
RIGHT([CL_DESCRIPTION] , 3)='PPC'
) AS DATA
ORDER BY REF

1 个答案:

答案 0 :(得分:0)

如果两个查询都由self运行,您可以使用类似:

 DECLARE @result TABLE(
 [ref] NVARCHAR(50),
 [CMP_CODE] NVARCHAR(50),
 [BANK_CODE] NVARCHAR(50),
 [TRANS_CODE] NVARCHAR(50),
 [Date] DATE,
 [AMOUNT] DECIMAL(10,2)
)
INSERT INTO @result
        ( ref ,
          CMP_CODE ,
          BANK_CODE ,
          TRANS_CODE ,
          Date ,
          AMOUNT
        )
SELECT 
      INTERNAL_REFERENCE as ref,
      CMP_CODE AS CMP_CODE,
      COUNTERPARTY_CODE AS BANK_CODE,
      TRANSACTION_CODE AS TRANS_CODE,
      CAST(CONVERT(varchar(10), AMO_END_DATE, 110) AS datetime) AS DATE,
      SUM([AMORTIZATION]) AS AMOUNT
FROM
  [SAGE_MTC_FRP].[dbo].[LOAN_SCHEDULE] inner join [SAGE_MTC_FRP].[dbo].    [LOANS]
  on [SAGE_MTC_FRP].[dbo].[LOAN_SCHEDULE].LOAN_ID=[SAGE_MTC_FRP].[dbo].    [LOANS].LOAN_ID
WHERE
  (AMO_END_DATE>=?) AND (BOOK_DATE<?) AND
  (TRANSACTION_CODE<>'CPCA' AND TRANSACTION_CODE<>'CPCF' AND  TRANSACTION_CODE<>'RENT') AND 
  IS_DELETED=0 AND VERSION_NUMBER=1 AND CMP_CODE='MTG'
GROUP BY 
  INTERNAL_REFERENCE, CMP_CODE, COUNTERPARTY_CODE, TRANSACTION_CODE,     AMO_END_DATE 

INSERT INTO @result
        ( ref ,
          CMP_CODE ,
          BANK_CODE ,
          TRANS_CODE ,
          Date ,
          AMOUNT
        )
SELECT
    CL_CODE as ref
    ,LEFT([ACC_CODE] , 3) AS CMP_CODE
    ,[COUNTERPARTY_CODE] AS BANK_CODE
    ,RIGHT([CL_DESCRIPTION] , 3) AS TRANS_CODE
    ,CAST(CONVERT(varchar(10), [END_DATE], 110) AS datetime) AS DATE
    ,[CL_AMOUNT] AS AMOUNT
FROM 
[SAGE_MTC_FRP].[dbo].[CREDIT_LINES]
WHERE 
(END_DATE>?) AND
RIGHT([CL_DESCRIPTION] , 3)='PPC'


SELECT * FROM @result ORDER BY ref