使用包含多个值的一个变量动态调整存储过程

时间:2016-07-18 19:25:48

标签: sql sql-server

我有一个存储过程,它接受一个输入参数,如下所示:

@Name NVARCHAR(50) = 'George W Bush'

如何动态调整where子句:

SELECT * FROM TABLE
WHERE CONTAINS(FIELD, 'George')
  AND CONTAINS(FIELD, 'W')
  AND CONTAINS(FIELD, 'Bush')

或者例如:

@Name NVARCHAR(50) = 'Harry Potter'

SELECT * FROM TABLE
WHERE CONTAINS(FIELD, 'Harry')
  AND CONTAINS(FIELD, 'Potter')

我发现这里的许多解决方案非常接近我所需要的,但我特别需要该字段来包含输入参数的每个子字符串。

4 个答案:

答案 0 :(得分:4)

CONTAINS可以处理包含ANDOR条件的多个单词:

 WHERE CONTAINS (FIELD, 'George AND W AND Bush')

所以你可以做的是将变量改为(假设它们总是以空格分隔):

DECLARE @Name NVARCHAR(50)
SET @Name = REPLACE('George W Bush', ' ', ' AND ')

SELECT * FROM TABLE
WHERE CONTAINS(FIELD, @Name)

答案 1 :(得分:0)

这是一种将名称拆分为可以使用的参数的方法,无论它是FirstName LastName还是FirstName MiddleName LastName。当然,如果你期望超过2个空格(三个名字),这将无法运作

DECLARE @Name NVARCHAR(50) = 'George W Bush' 
DECLARE @Spaces int = (SELECT LEN(@Name)-LEN(REPLACE(@Name, ' ', '')))

DECLARE @FirstName varchar(50) = null
DECLARE @MiddleName varchar(50) = null
DECLARE @LastName varchar(50) = null

IF @Spaces = 2 
    BEGIN
        SET @FirstName = LEFT(@Name,CHARINDEX(' ',@Name))
        SET @MiddleName = SUBSTRING(@Name,CHARINDEX(' ',@Name) + 1,1)
        SET @LastName = RIGHT(@Name,CHARINDEX(' ',REVERSE(@Name)))
    END
ELSE IF @Spaces = 1 
    BEGIN
        SET @FirstName = LEFT(@Name,CHARINDEX(' ',@Name))
        SET @LastName = RIGHT(@Name,CHARINDEX(' ',REVERSE(@Name)))
    END


SELECT @FirstName, @MiddleName, @LastName

答案 2 :(得分:0)

如果您的表格是全文索引的,

CONTAINS会有效,但如果不是,请考虑以下事项:

-- Here's my test data
declare @t table
(
   text nvarchar(max)
)

insert @t values('I''m just wild about Harry')
              , ('Sally Potter is important in British theater')
              , ('I love the "Harry Potter" books')


-- Here are our keywords:
declare @Name varchar(max) = 'Harry Potter'

-- To make this work, we need to convert the substrings into tabular form.  
-- so here's how we'll do that. 

declare @NameXml xml 

set @NameXml = convert(xml, '<substrings><substring>' + 
               replace(@Name, ' ', '</substring><substring>') +
               '</substring></substrings>')

-- That gives us an XML variable with each substring as a separate 
-- element.
;
with mockTable as 
(
    select  Keyword = N.a.value('.[1]', 'varchar(100)')
    from    @NameXml.nodes('/substrings/substring') as N(a)
)
-- Now that we have a "mock table", we can use the ALL operator to 
-- confirm that each string in our mock table is found in the
-- records we return.
select  *
from    @t
where   1 = ALL (
    select  case 
            when [text] like '%' + Keyword + '%'
            then 1
            else 0
            end
    from mockTable
    )

答案 3 :(得分:0)

就像Ann L.所说,CONTAINS和FREETEXT仅在您已经在基础表上创建了FULLTEXT INDEX时才起作用。 根据你需要这个字符串解析操作,为什么不创建一个用户定义的函数来处理它,并在你的Sproc中使用它。顺便说一句,我真的不想使用外卡。 :)

CREATE FUNCTION [dbo].[parsing]
(
    @String NVARCHAR(2000),
    @Delimiter NVARCHAR(5)
)
RETURNS @Rtn TABLE
(
    ID INT IDENTITY(1,1),
    Seg NVARCHAR(50)
)
AS
BEGIN
    WHILE(CHARINDEX(@Delimiter,@String)>0)
    BEGIN
        INSERT INTO @Rtn (Seg)
        SELECT LTRIM(RTRIM(SUBSTRING(@String,1,CHARINDEX(@Delimiter,@String)-1)))
        SET @String = SUBSTRING(@String,CHARINDEX(@Delimiter,@String)+LEN(@Delimiter),LEN(@String))
    END
    INSERT INTO @Rtn (Seg)
    SELECT LTRIM(RTRIM(@String))
END