日期:2014-05-18  浏览次数:20461 次

SQL存储过程的模糊查询问题
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Pr_Name_GetDocuments]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[Pr_Name_GetDocuments]
(
@Title varchar(50)
)
AS

SELECT
  Document.*,Staff.RealName

FROM  
  Document ,Staff
WHERE  
  Staff.Staff_ID = Document.UserID and Document.Title like '%'+@Title+'%'
 

END



这样写怎么一直提示'%' 附近有语法错误。。。菜鸟求教大神啊。。。

------解决方案--------------------
SQL code

exec('SELECT
  Document.*,Staff.RealName

FROM   
  Document ,Staff
WHERE   
  Staff.Staff_ID = Document.UserID and charindex('+@title+'Document.Title)>0')
 

改成这样

------解决方案--------------------
Staff.Staff_ID = Document.UserID and Document.Title like ''%''+@Title+''%''

------解决方案--------------------
探讨

SQL code

exec('SELECT
Document.*,Staff.RealName

FROM
Document ,Staff
WHERE
Staff.Staff_ID = Document.UserID and charindex('+@title+'Document.Title)>0')


改成这样