能否請你幫我提高了表值函數:提高表值函數性能
Create Function [dbo].[xxx](@InvoiceId numeric(18 ,0) ,@InvoiceType int ,@ReceiptAccOwner int)
Returns Table
As
Return(
Select * From(
Select N'InvoiceReceiptNo' + Cast(ROW_NUMBER() over(order by ir.ReceiptRecID) as nvarchar(10)) As row
,ir.StockHoldingInvoiceID As InvoiceId
,ir.ReceiptNo As ReceiptValue
,ir.ReceiptAccOwner
from InvoiceReceipt As ir
Where ir.ReceiptAccOwner = @ReceiptAccOwner
And (
(ir.StockHoldingInvoiceID = @InvoiceId And @InvoiceType in(1 ,6 ,8))
or(ir.THCInvoiceID = @InvoiceId And @InvoiceType = 2)
or(ir.EvaluatInvoiceID = @InvoiceId And @InvoiceType = 3)
or(ir.StripInvoiceID = @InvoiceId And @InvoiceType = 4)
or(ir.EvaluationExportInvoiceID = @InvoiceId And @InvoiceType = 5)
or(ir.ServiceInvoiceID = @InvoiceId And @InvoiceType = 7)
or(ir.MovementInvoiceId = @InvoiceId And @InvoiceType = 10)
or(ir.InterchangeReplicaInvoice = @InvoiceId And @InvoiceType = 11)
or(ir.MovementInvoiceId = @InvoiceId And @InvoiceType = 12)
or(ir.BLConsigneeChangeRequestInvoiceID = @InvoiceId And @InvoiceType = 15)
)
UNION All
Select N'InvoiceReceiptPrice' + Cast(ROW_NUMBER() over(order by ir.ReceiptRecID) as nvarchar(10)) As row
,ir.StockHoldingInvoiceID
,Cast(IsNull(ir.ReceiptPrice ,0) as nvarchar(20)) As ReceiptValue
,ir.ReceiptAccOwner
from InvoiceReceipt As ir
Where ir.ReceiptAccOwner = @ReceiptAccOwner
And (
(ir.StockHoldingInvoiceID = @InvoiceId And @InvoiceType in(1 ,6 ,8))
or(ir.THCInvoiceID = @InvoiceId And @InvoiceType = 2)
or(ir.EvaluatInvoiceID = @InvoiceId And @InvoiceType = 3)
or(ir.StripInvoiceID = @InvoiceId And @InvoiceType = 4)
or(ir.EvaluationExportInvoiceID = @InvoiceId And @InvoiceType = 5)
or(ir.ServiceInvoiceID = @InvoiceId And @InvoiceType = 7)
or(ir.MovementInvoiceId = @InvoiceId And @InvoiceType = 10)
or(ir.InterchangeReplicaInvoice = @InvoiceId And @InvoiceType = 11)
or(ir.MovementInvoiceId = @InvoiceId And @InvoiceType = 12)
or(ir.BLConsigneeChangeRequestInvoiceID = @InvoiceId And @InvoiceType = 15)
)
) As tb
pivot(
Max(ReceiptValue) --,Sum(ReceiptPrice)
For row in ([InvoiceReceiptNo1] ,[InvoiceReceiptNo2] ,[InvoiceReceiptNo3] ,[InvoiceReceiptNo4] ,[InvoiceReceiptNo5] ,[InvoiceReceiptNo6]
,[InvoiceReceiptPrice1] ,[InvoiceReceiptPrice2] ,[InvoiceReceiptPrice3] ,[InvoiceReceiptPrice4] ,[InvoiceReceiptPrice5] ,[InvoiceReceiptPrice6])
) piv
)
我在存儲過程中使用該功能外適用於SELECT語句, 這個功能應該只返回一個記錄每張發票。
XML執行計劃: 從[DBO]選擇* [fn_GetInvoiceReceiptInfo](149735,1,1) https://anotepad.com/notes/wh34ey
你可以把執行計劃放在pastetheplan.com並給我們一個鏈接?這將幫助我們看到發生了什麼。 –
@RichBenner pastetheplan.com不能爲我開放。 – Iraj
嘗試此鏈接https://www.brentozar.com/pastetheplan/ –