我認爲storeno
是整數,即使它不是你可以遵循相同的原則爲任何其他數據類型。這將是語法簡單的方法,但看出來的執行計劃:
CREATE PROCEDURE [dbo].[sp_ReportTotals] (
@fromdate VARCHAR(10),
@todate VARCHAR(10),
@storeno INT -- if you want all stores send NULL as @storeno parameter, if you want specific store send its storeno
)
AS
BEGIN
SELECT
[POS_REPORT].[dbo].[Txn_Daily_Totals].[storeno],
[POS_REPORT].[dbo].[Txn_Daily_Totals].[Description],
[POS_REPORT].[dbo].[Txn_Daily_Totals].[transactiondate],
[POS_REPORT].[dbo].[Txn_Daily_Totals].[amount] AS 'pos',
[POS_REPORT].[dbo].[SAP_FI_INBOUND_DAILY_TOTALS].[amount] AS 'ecc',
CASE WHEN [POS_REPORT].[dbo].[Txn_Daily_Totals].[amount] = [POS_REPORT].[dbo].[SAP_FI_INBOUND_DAILY_TOTALS].[amount] THEN 'BALANCED' ELSE 'UNBALANCED' END AS 'status',
[POS_REPORT].[dbo].[comments].[responce],
[POS_REPORT].[dbo].[comments].[comment]
FROM
[POS_REPORT].[dbo].[Txn_Daily_Totals]
LEFT JOIN
[POS_REPORT].[dbo].[SAP_FI_INBOUND_DAILY_TOTALS] ON
[POS_REPORT].[dbo].[Txn_Daily_Totals].[storeno] +
[POS_REPORT].[dbo].[Txn_Daily_Totals].[transactiondate] =
[POS_REPORT].[dbo].[SAP_FI_INBOUND_DAILY_TOTALS].[storeno] +
[POS_REPORT].[dbo].[SAP_FI_INBOUND_DAILY_TOTALS].[transactiondate]
LEFT JOIN
[POS_REPORT].[dbo].[comments] ON
[POS_REPORT].[dbo].[Txn_Daily_Totals].[storeno] +
[POS_REPORT].[dbo].[Txn_Daily_Totals].[transactiondate] =
[POS_REPORT].[dbo].[comments].[storeno] +
[POS_REPORT].[dbo].[comments].[transactiondate]
WHERE
[POS_REPORT].[dbo].[Txn_Daily_Totals].[transactiondate] = @fromdate AND
[POS_REPORT].[dbo].[Txn_Daily_Totals].[transactiondate] <= @todate AND
(@storeno IS NULL OR [POS_REPORT].[dbo].[Txn_Daily_Totals].[storeno] = @storeno)
ORDER BY
[POS_REPORT].[dbo].[Txn_Daily_Totals].[Description],
[POS_REPORT].[dbo].[Txn_Daily_Totals].[transactiondate]
END
那些可能產生更有利的執行計劃可能是另一種方法:
CREATE PROCEDURE [dbo].[sp_ReportTotals] (
@fromdate VARCHAR(10),
@todate VARCHAR(10),
@storeno INT -- if you want all stores send NULL as @storeno parameter, if you want specific store send its storeno
)
AS
BEGIN
SELECT
[POS_REPORT].[dbo].[Txn_Daily_Totals].[storeno],
[POS_REPORT].[dbo].[Txn_Daily_Totals].[Description],
[POS_REPORT].[dbo].[Txn_Daily_Totals].[transactiondate],
[POS_REPORT].[dbo].[Txn_Daily_Totals].[amount] AS 'pos',
[POS_REPORT].[dbo].[SAP_FI_INBOUND_DAILY_TOTALS].[amount] AS 'ecc',
CASE WHEN [POS_REPORT].[dbo].[Txn_Daily_Totals].[amount] = [POS_REPORT].[dbo].[SAP_FI_INBOUND_DAILY_TOTALS].[amount] THEN 'BALANCED' ELSE 'UNBALANCED' END AS 'status',
[POS_REPORT].[dbo].[comments].[responce],
[POS_REPORT].[dbo].[comments].[comment]
INTO #temp
FROM
[POS_REPORT].[dbo].[Txn_Daily_Totals]
LEFT JOIN
[POS_REPORT].[dbo].[SAP_FI_INBOUND_DAILY_TOTALS] ON
[POS_REPORT].[dbo].[Txn_Daily_Totals].[storeno] +
[POS_REPORT].[dbo].[Txn_Daily_Totals].[transactiondate] =
[POS_REPORT].[dbo].[SAP_FI_INBOUND_DAILY_TOTALS].[storeno] +
[POS_REPORT].[dbo].[SAP_FI_INBOUND_DAILY_TOTALS].[transactiondate]
LEFT JOIN
[POS_REPORT].[dbo].[comments] ON
[POS_REPORT].[dbo].[Txn_Daily_Totals].[storeno] +
[POS_REPORT].[dbo].[Txn_Daily_Totals].[transactiondate] =
[POS_REPORT].[dbo].[comments].[storeno] +
[POS_REPORT].[dbo].[comments].[transactiondate]
WHERE
[POS_REPORT].[dbo].[Txn_Daily_Totals].[transactiondate] = @fromdate AND
[POS_REPORT].[dbo].[Txn_Daily_Totals].[transactiondate] <= @todate
ORDER BY
[POS_REPORT].[dbo].[Txn_Daily_Totals].[Description],
[POS_REPORT].[dbo].[Txn_Daily_Totals].[transactiondate]
IF(@storeno IS NULL)
BEGIN
SELECT *
FROM #temp
END
ELSE
BEGIN
SELECT *
FROM #temp
WHERE storeno = @storeno
END
END
這種方法涉及兩個SELECT語句和臨時表,但它可能執行得更快(即可能有更好的執行計劃)。在SP的生產版本中使用其中的任何版本之前,請檢查執行計劃和查詢成本指標,並確定哪種方法對您更好。
對不起,對於MSSQL服務器2008 – Skyboard