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 项必须出现在选择列表中。