2013-06-20 58 views
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時:以便能夠通過與聯盟的SQL Server 2008中

消息4104,級別16,狀態1,過程spReport,41號線
多部分組成的標識符「頁。 ProductId「無法綁定。
Msg 104,Level 16,State 1,Procedure spQueryPaymentDetailReport,Line 41
如果語句包含UNION,INTERSECT或EXCEPT運算符,則ORDER BY項必須出現在選擇列表中。

回答

1

您不能在ORDER BY中引用p.ProductId,因爲第一個子查詢沒有p數據集。

您不能在ORDER BY中引用ProductId列,因爲在組合結果集中沒有該名稱的列。

爲了解決這些問題,你可以添加一個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 
;

第一行分配MaxInt ID值,因爲據我所知,它是標題行,必須按照指定的ORDER BY p.ProductId DESC子句先行(因爲我假定ProductIdint當然)。

+0

謝謝@Andriy M,這完美地解決了我的問題。 –