如何识别where子句匹配的模式

时间:2012-02-19 11:59:51

标签: sql sql-server tsql stored-procedures

我需要有关T-SQL的帮助。我的SQL Server中有一个存储过程,其查询(示例)如下所示:

Select empid, empname, sal from emp
  where empname like '%jo%'
  and   empname like '%el%'

让我们说,上面的查询返回如下所示的记录:

empid empname sal   
17    john    $1000
45    elena   $2000

我需要在上面的select语句中添加另一列,对于'jo'记录应该%jo%,对'el'记录应该%el%。例如:

empid empname sal     SearchString
17    john    $1000   jo
45    elena   $2000   el

这可能吗?

谢谢

4 个答案:

答案 0 :(得分:1)

类似于此的东西将起作用,注意您可以将参数传递给存储的proc

declare @empname varchar(255)
set @empname = 'jo'

Select empid, empname, sal, @empname as searchstring from emp 
  where empname like '%'+@empname+'%'

答案 1 :(得分:1)

为了满足多种条件,您可以使用CTE

  • 创建内存表
  • 将实际表连接到此内存表
  • 检索select子句中的条件

SQL声明

;WITH Conditions (SearchString) AS (
  SELECT 'jo' UNION ALL
  SELECT 'el'
)
SELECT  empid
        , empname
        , sal
        , Searchstring
FROM    emp e
        INNER JOIN Conditions c ON e.empname LIKE '%' + c.SearchString + '%'

测试脚本

;WITH Conditions (SearchString) AS (
  SELECT 'jo' UNION ALL
  SELECT 'el'
)
, emp (empid, empname, sal) AS (
    SELECT 17, 'john', 1000 UNION ALL
    SELECT 45, 'elena', 2000
)
SELECT  empid
        , empname
        , sal
        , Searchstring
FROM    emp e
        INNER JOIN Conditions c ON e.empname LIKE '%' + c.SearchString + '%'

答案 2 :(得分:0)

看起来您想知道哪个搜索字符串与任何给定的输出行匹配。折扣匹配多个搜索字符串的可能性,例如: empname'Joel',以下内容应该有效:

declare @emp as table (empid int, empname varchar(10), sal money)
insert into @emp ( empid, empname, sal ) values
  ( 17, 'john', 1000 ),
  ( 45, 'elena', 2000 )

select empid, empname, sal,
  case
    when empname like '%jo%' then 'jo'
    when empname like '%el%' then 'el'
    else '?'
    end as SearchString
  from @emp
  where empname like '%jo%' or empname like '%el%'

答案 3 :(得分:0)

首先,如果你需要解析以逗号分隔的字符串,你应该有一个split函数。这是我使用的那个:

CREATE FUNCTION [dbo].[SplitStrings]
(
   @List       NVARCHAR(MAX)
)
RETURNS TABLE
AS
   RETURN ( SELECT Item FROM (
     SELECT Item = x.i.value('(./text())[1]', 'nvarchar(max)')
        FROM ( SELECT [XML] = CONVERT(XML, '<i>' 
                + REPLACE(@List, ',', '</i><i>') 
                + '</i>').query('.') ) AS a 
        CROSS APPLY [XML].nodes('i') AS x(i)
    ) AS y WHERE Item IS NOT NULL
   );
GO

现在假设您想要在多个匹配的情况下选择任意匹配,您可以说:

CREATE PROCEDURE dbo.Search
    @SearchStrings NVARCHAR(MAX)
AS
BEGIN
    SET NOCOUNT ON;

    ;WITH x AS (
        SELECT e.empid, e.empname, e.sal, SearchString = s.Item,
            rn = ROW_NUMBER() OVER (PARTITION BY e.empid ORDER BY s.Item)
        FROM dbo.emp AS e
        INNER JOIN dbo.SplitStrings(@SearchStrings) AS s
        ON e.empname LIKE '%' + s.Item + '%'
    ) 
    SELECT empid, empname, sal, SearchString
    FROM x WHERE rn = 1 ORDER BY empid;
END
GO

(这将返回@SearchStrings参数中的第一个字母匹配,而不是逗号分隔字符串的第一个匹配。)

如果您只是想要所有行,即使员工出现多个传入模式(如果您的参数为'john',则表示'jo,oh'将返回两次),可以这么说:

CREATE PROCEDURE dbo.Search
    @SearchStrings NVARCHAR(MAX)
AS
BEGIN
    SET NOCOUNT ON;

    SELECT e.empid, e.empname, e.sal, SearchString = s.Item
      FROM dbo.emp AS e
      INNER JOIN dbo.SplitStrings(@SearchStrings) AS s
      ON e.empname LIKE '%' + s.Item + '%'
      ORDER BY empid;
END
GO

如果在多个模式与员工匹配的情况下需要以逗号分隔的匹配列表,请在您的要求中说明。这样做还有点工作。