2009-07-06 41 views
0

我有以下查詢哪些工作正常,但我也想使用like關鍵字獲得記錄。我的查詢如下,我想使用像關鍵字

USE [POBox] 
GO 
/****** Object: StoredProcedure [dbo].[test] Script Date: 07/06/2009 12:55:39 ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
ALTER PROCEDURE [dbo].[test] --'DateTime','1','10' 
(
@p_SortExpression NVARCHAR(100), 
@p_StartIndex INT, 
@p_MaxRows INT 
) 
AS 
SET NOCOUNT ON; 
IF LEN(@p_SortExpression)= 0 
SET @p_SortExpression = 'DateTime DESC' 

DECLARE @Sql NVARCHAR(4000) 
SET @Sql ='SELECT ID, DateTime, Subject, CreatedBy, ToReceipientID , ReceiverStatus 
      FROM 
      (
      SELECT ID, DateTime, Subject,CreatedBy, ToReceipientID, ReceiverStatus, 
      ROW_NUMBER() OVER(ORDER BY ' + @p_SortExpression + ') AS Indexing 
      FROM ComposeMail 
      WHERE (Subject Like '%+ test +%') 
      ) 
      AS NewDataTable 
      WHERE Indexing > '+ CONVERT(NVARCHAR(10), @p_StartIndex) + ' AND Indexing<=(' + CONVERT (NVARCHAR(10),@p_StartIndex)+'+'+ CONVERT(NVARCHAR(10),@p_MaxRows)+')' 

      EXEC sp_executesql @sql 

任何人都可以幫忙嗎?我如何使用像,我也想用它作爲參數。

回答

2

我想你的要求是什麼:如何在sql中使用like命令?

爲了您的代碼位,添加一個新的參數:

@SearchText nvarchar(100)

然後更換:

WHERE (Subject Like '%+ test +%')

WHERE Subject (Like ''%'+ ISNULL(test, '') +'%''')

欲瞭解更多信息,請看這裏: http://msdn.microsoft.com/en-us/library/ms179859.aspx

+0

感謝你的朋友我得到它與你顯示給我的語法 – 2009-07-06 10:20:29

0

不要你已經有喜歡應用於:

WHERE (Subject Like '%+ test +%')

不知道是什麼問題,或者如果我沒有完全理解它。