我认为那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 的生产版本中使用任何这些之前,请检查执行计划和查询成本指标,并确定哪种方法更适合您。