【发布时间】:2011-07-08 01:15:22
【问题描述】:
这是我在我们制作的列表中一直看到的。 必须为 2 列做一个过滤器(我举了一个较短的例子,我们有 50 列的列表,看起来一样...)
我想知道是否有办法以某种不同的方式做到这一点。 你们用什么来过滤表中的记录?
/* parameters */
@CostNumber = ...
@Description = ...
/* parameters */
SET @strSQL = '
SELECT [CostLevelId], [CostNumber], [Description]
FROM [CostLevel]
WHERE 1 = 1 ' /* this looks weird, but we'll add the "WHERE" stuff easier */
IF (@CostNumber IS NOT NULL)
BEGIN
SET @strSQL = @strSQL + ' AND [CostNumber] LIKE ''%' + REPLACE(@CostNumber,'''','''''') + '%'' '
END
IF (@Description IS NOT NULL)
BEGIN
SET @strSQL = @strSQL + ' AND [Description] LIKE ''%' + REPLACE(@Description,'''','''''') + '%'' '
END
EXEC (@strSQL)
【问题讨论】:
标签: sql sql-server-2005 filter dynamic-sql