0
CREATE PROCEDURE [dbo].[spReport]
        @FromDate DATETIME = NULL,
        @ToDate DATETIME = NULL,
        @TenantID int ,
        @BusinessUnitId int
AS
BEGIN
  Declare @listStr Varchar(max), @listValue Varchar(max)

  Select 
     @listStr = COALESCE(@listStr+',' ,'') + FieldLabel 
  From 
     (Select Distinct Top 100 
         FieldLabel, ControlTypeId 
      From 
         PaymentCustomFieldDefinitions PCFD
      Inner Join 
         Product P On P.Id = PCFD.ProductId
      Where 
         P.TenantId = @TenantId
      Order By 
         ControlTypeId Desc) R

   Set @listStr = ',' + @listStr
   Set @listStr = IsNull(@listStr, '')

   Select       
       'Confirmation Number,Business Unit,Bank Account,Merchant Account,Product Name,Payment Date,Payment Time,Total Amount,Status,First Name,Last Name,Payment Method' + @listStr  
   Union
   SELECT       
      p.ConfirmationNumber + ',' + bu.Name + ',' + p.TenantBankAccountName + ',' + Case When ma.Name IS NULL then '' ELSE ma.Name END  + ',' + pd.Name + ',' + 
      cast(P.PaymentDate as Varchar(11)) + ',' + convert(VARCHAR(8), P.PaymentDate,108) + ',' + Cast(p.TotalDue As Varchar(20)) + ',' + p.PaymentStatusText + ',' + p.PayorFirstName + ',' + p.PayorLastName + ',' + p.PaymentMethodText + ',' + [dbo].[GetPaymentReferenceAndCustomFields](p.Id,@listStr) 
   FROM 
      Payment p 
   INNER JOIN 
      Product pd ON p.ProductId = pd.Id
   INNER JOIN 
      BusinessUnit bu ON pd.BusinessUnitId = bu.Id 
   INNER JOIN 
      ProductDetailPayment pdp ON p.ProductId = pdp.ProductId
   LEFT OUTER JOIN 
      MerchantAccount ma ON  ma.Id = pdp.MerchantAccountId   
   WHERE 
      p.PaymentDate BETWEEN @FromDate AND @ToDate 
      AND p.TenantId = @TenantId  AND pd.BusinessUnitId= @BusinessUnitId
   ORDER BY 
      p.ProductId desc
END

使用时出现此错误Order by

消息 4104,级别 16,状态 1,过程 spReport,第 41 行
无法绑定多部分标识符“p.ProductId”。
消息 104,级别 16,状态 1,过程 spQueryPaymentDetailReport,第 41 行
如果语句包含 UNION、INTERSECT 或 EXCEPT 运算符,则 ORDER BY 项必须出现在选择列表中。

4

1 回答 1

1

您不能p.ProductId在 ORDER BY 中引用,因为第一个子查询没有p数据集。

您不能引用ProductIdORDER BY 中的列,因为在组合结果集中没有该名称的列。

要解决这些问题,您可以添加一个ProductId列,如果最终输出必须包含一个列,如您的查询中一样,请使用派生表:

SELECT
  p.CSV
FROM (
    Select
        ProductId = 2147483647,
        CSV = 'Confirmation Number,Business Unit,Bank Account,Merchant Account,Product Name,Payment Date,Payment Time,Total Amount,Status,First Name,Last Name,Payment Method' + @listStr
    Union
    SELECT
       p.ProductId,
       p.ConfirmationNumber + ',' + bu.Name + ',' + p.TenantBankAccountName + ',' + Case When ma.Name IS NULL then '' ELSE ma.Name END  + ',' + pd.Name + ',' +
       cast(P.PaymentDate as Varchar(11)) + ',' + convert(VARCHAR(8), P.PaymentDate,108) + ',' + Cast(p.TotalDue As Varchar(20)) + ',' + p.PaymentStatusText + ',' + p.PayorFirstName + ',' + p.PayorLastName + ',' + p.PaymentMethodText + ',' + [dbo].[GetPaymentReferenceAndCustomFields](p.Id,@listStr)
    FROM
       Payment p
    INNER JOIN
       Product pd ON p.ProductId = pd.Id
    INNER JOIN
       BusinessUnit bu ON pd.BusinessUnitId = bu.Id
    INNER JOIN
       ProductDetailPayment pdp ON p.ProductId = pdp.ProductId
    LEFT OUTER JOIN
       MerchantAccount ma ON  ma.Id = pdp.MerchantAccountId
    WHERE
       p.PaymentDate BETWEEN @FromDate AND @ToDate 
       AND p.TenantId = @TenantId  AND pd.BusinessUnitId= @BusinessUnitId
) p
ORDER BY
   p.ProductId desc
;

第一行被分配了MaxIntID 值,因为据我了解,它是标题行,必须根据指定的ORDER BY p.ProductId DESC子句先行(当然,因为我假设它ProductId是一个int)。

于 2013-06-21T06:49:20.780 回答