4

我有 2 个输入参数,我想搜索用户名@Username的一部分和名称的一部分是@Name该用户的用户名的@Username一部分和名称的一部分@Name

SELECT *  
FROM tbl_answer
WHERE 
     an_del = 0 
     AND u_username = ISNULL(LIKE %@Username%, u_username) OR 
         u_name = ISNULL(LIKE @Name, u_name) 

我如何LIKE在这个存储过程中使用?

4

2 回答 2

3
CREATE PROC dbo.SearchAnswers
@Username nvarchar(20),
@Name nvarchar(20)
AS
SELECT *  
FROM tbl_answer
WHERE an_del=0 and u_username LIKE '%' + ISNULL(@Username, u_username) + '%'
  OR u_name LIKE ISNULL(@Name, u_name)

SQLServer2008 示例

于 2013-05-17T11:01:00.540 回答
1

您需要使用动态sql,例如sql server:

create procedure MyProc
(
  @Username varchar(30),
  @Name varchar(30)
)
as
begin
   exec ('SELECT * from tbl_answer where an_del=0 and 
           u_username=isnull(like ''%'+@Username+'%'',u_username) 
             or u_name=isnull(like '''+@Name+''',u_name)') 
end
于 2013-05-17T10:28:08.247 回答