需要帮助的人。替代SSRS中的动态查询
我有显示所有记录的存储过程。
SELECT
Entity.Name as [ENTITY],
Product.Name AS [Product Name],
convert(date, whseintg.TrnDate) as TrnDate,
DOGR.AppNo,
DOGR.TrnNo,
DOGR.TrnType,
DOGR.StkId,
DOGR_D.ProdId,
DOGR_D.Qty,
DOGR_D.QtyIn,
DOGR_D.UPrice,
Ratio.Ratio
FROM Entity WITH (NOLOCK),
Product WITH (NOLOCK),
DOGR WITH (NOLOCK),
DOGR_D WITH (NOLOCK),
Ratio WITH (NOLOCK),
whseintg WITH (Nolock)
WHERE (DOGR_D.ProdId = Product.ProdId) and
(DOGR.TrnType = DOGR_D.TrnType) and
(DOGR.AppNo = DOGR_D.AppNo) and
(DOGR_D.RatioId = Ratio.Ratioid) and
(DOGR.TrnType = whseintg.TrnType) and
(DOGR.Appno = whseintg.TrnNo) and
(DOGR.TrnNo is not null) and
((dbo.DOGR.TrnType = 'SCR')) and
(dbo.DOGR.LocID = dbo.Entity.LocID)
现在,我有一定的参数,如在报告中的设计视图@FromProductName
和@ToProductName
。
我不想使用动态查询,因为它会对应用性能产生影响。我想的是,如果在这两个变量传递的值,查询将是这样的:
SELECT
Entity.Name as [ENTITY],
Product.Name AS [Product Name],
convert(date, whseintg.TrnDate) as TrnDate,
DOGR.AppNo,
DOGR.TrnNo,
DOGR.TrnType,
DOGR.StkId,
DOGR_D.ProdId,
DOGR_D.Qty,
DOGR_D.QtyIn,
DOGR_D.UPrice,
Ratio.Ratio
FROM Entity WITH (NOLOCK),
Product WITH (NOLOCK),
DOGR WITH (NOLOCK),
DOGR_D WITH (NOLOCK),
Ratio WITH (NOLOCK),
whseintg WITH (Nolock)
WHERE (DOGR_D.ProdId = Product.ProdId) and
(DOGR.TrnType = DOGR_D.TrnType) and
(DOGR.AppNo = DOGR_D.AppNo) and
(DOGR_D.RatioId = Ratio.Ratioid) and
(DOGR.TrnType = whseintg.TrnType) and
(DOGR.Appno = whseintg.TrnNo) and
(DOGR.TrnNo is not null) and
((dbo.DOGR.TrnType = 'SCR')) and
(dbo.DOGR.LocID = dbo.Entity.LocID)
and (DOGR_D.ProdId between @FromProdID and @ToProdID)
否则,它会像原来的查询。那可能吗?
并请停止使用隐式语法。这是一种很差的编程技术,很容易导致错误的结果或意外的交叉连接,因为连接文件夹是从连接中指定的。此外,如果您需要更改为左连接,则需要重写整个qwuery因为您不应该混合使用显式连接和隐式连接,否则您很可能会得到错误的答案。在复杂的报表查询中,这是一种特别糟糕的做法,因为您可能会有许多联结。这个语法在20年前被替换了,为什么你还在考虑使用它? – HLGEM 2012-03-13 17:34:42