1

所以这对你来说应该是一个非常基本的。我有一个通过日期查找的存储过程。我想添加一个 @storeno 变量,它可以是一个或所有的疮。我该怎么做?

USE [POS_REPORT]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[sp_ReportTotals] (
@fromdate VARCHAR(10),
@todate VARCHAR(10)
)
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
ORDER BY
[POS_REPORT].[dbo].[Txn_Daily_Totals].[Description],
[POS_REPORT].[dbo].[Txn_Daily_Totals].[transactiondate]
END

GO

任何帮助,将不胜感激。

4

2 回答 2

0
CREATE PROCEDURE [dbo].[sp_ReportTotals] (
--your paramets
@storeno your_data_type
....
--Changed WHERE clause
WHERE
  [POS_REPORT].[dbo].[Txn_Daily_Totals].[transactiondate]  = @fromdate AND
  [POS_REPORT].[dbo].[Txn_Daily_Totals].[transactiondate] <= @todate AND
  [POS_REPORT].[dbo].[Txn_Daily_Totals].[storeno] = ISNULL(@storeno, [POS_REPORT].[dbo].[Txn_Daily_Totals].[storeno])
于 2012-11-25T15:39:37.927 回答
0

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

于 2012-11-21T07:54:23.483 回答