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" をバインドできませんでした。
Msg 104、Level 16、State 1、Procedure spQueryPaymentDetailReport、Line 41
ステートメントに UNION、INTERSECT、または EXCEPT 演算子が含まれている場合、ORDER BY アイテムを選択リストに含める必要があります。

4

1 に答える 1