WHERE中的SSRS参数

时间:2016-07-07 14:38:14

标签: sql reporting-services parameters ssrs-2016

我认为这很简单,但我无法找到一种有效的方法。

基本上我想通过WHERE子句中输入的参数IE进行报告搜索。

例如,下面的参数是@ColumnName和@SearchTerm

SELECT
OITM.[ItemCode] AS [Item Code],
OITM.[ItemName] AS [Item Name],
OITM.[FrgnName] AS [Merchandise Type],
OITM.[OnHand] AS [In Stock],
OITM.[IsCommited] AS [Customer Reserved], 
OITM.[Onorder] AS [On Order],
OITW.[AvgPrice] AS [Cost]

FROM OITW
 INNER JOIN OITM ON OITM.[ItemCode] = OITW.[ItemCode]

WHERE
OITM.(@ColumnName) LIKE '%(@SearchTerm)%'

这一定是可能的吗?非常感谢。

2 个答案:

答案 0 :(得分:2)

我并不总是鼓励使用动态SQL,但以下内容将完成您要完成的任务。

DECLARE @ColumnName varchar(10) = 'ItemName', @SearchTerm varchar(5) = 'pat', @sql varchar(1000) = ''

SELECT @sql = 'SELECT
OITM.[ItemCode] AS [Item Code],
OITM.[ItemName] AS [Item Name],
OITM.[FrgnName] AS [Merchandise Type],
OITM.[OnHand] AS [In Stock],
OITM.[IsCommited] AS [Customer Reserved], 
OITM.[Onorder] AS [On Order],
OITW.[AvgPrice] AS [Cost]

FROM OITW
 INNER JOIN OITM ON OITM.[ItemCode] = OITW.[ItemCode]

WHERE OITM.' + @ColumnName + ' LIKE ''%' + @SearchTerm + '%'''

EXEC (@sql)

以下是我将如何做到这一点,但你可能有理由不想这样做。由你决定。

SELECT
OITM.[ItemCode] AS [Item Code],
OITM.[ItemName] AS [Item Name],
OITM.[FrgnName] AS [Merchandise Type],
OITM.[OnHand] AS [In Stock],
OITM.[IsCommited] AS [Customer Reserved], 
OITM.[Onorder] AS [On Order],
OITW.[AvgPrice] AS [Cost]

FROM OITW
 INNER JOIN OITM ON OITM.[ItemCode] = OITW.[ItemCode]

WHERE (OITM.ItemName LIKE '%' + @SearchTerm + '%'
    OR OITM.FrgnName LIKE '%' + @SearchTerm + '%')...
祝你好运!

答案 1 :(得分:1)

试试这个:

SELECT
OITM.[ItemCode] AS [Item Code],
OITM.[ItemName] AS [Item Name],
OITM.[FrgnName] AS [Merchandise Type],
OITM.[OnHand] AS [In Stock],
OITM.[IsCommited] AS [Customer Reserved], 
OITM.[Onorder] AS [On Order],
OITW.[AvgPrice] AS [Cost]

FROM OITW
 INNER JOIN OITM ON OITM.[ItemCode] = OITW.[ItemCode]

WHERE
OITM.(@ColumnName) LIKE '%' + @SearchTerm + '%'
  

请注意,您的参数必须是文字类型。

如果有帮助,请告诉我。