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項必須出現在選擇列表中。
謝謝@Andriy M,這完美地解決了我的問題。 –