大哥们:
    我有一个sql语句不会写。要求是这样的:在”电影名称“中查找,用户输入的关键,如果有模糊相匹配的就全部显示出来。
   
    比如查找”电影名称“字段中,凡是”上海“的电影记录,怎么写?我写了一个但执行时,没什么反映,鼓计存储过程写错了。select count(id) from Film_Info  where FilmName like "%@SearchChar %"   and  FilmClass=@FilmClass2@SearchChar是用户输入的关键字,参数
这是统计 相匹配的电影记录的个数。但执行就是没反映。等于没有执行一下,请大家帮我看看有错吗??

解决方案 »

  1.   

    string sel="select count(id) from Film_Info  where FilmName like '%"+@SearchChar+"%'   and  FilmClass=@FilmClass2 ";
      

  2.   

    http://dev.csdn.net/develop/article/69/69396.shtm
      

  3.   

    select count(id) from Film_Info  where FilmName like "%@SearchChar %"   and  FilmClass=@FilmClass2
    你这句语句大概功能是:查找所有(电影)文件名中包函SearchChar内容的文件个数。对吧?
      

  4.   

    但是很不幸,你的SQL语句好像是出错也。
      

  5.   

    string sel="select count(id) from Film_Info  where FilmName like '%"+@SearchChar+"%'   and  FilmClass='"+ @FilmClass2 +"'";
      

  6.   

    还是不行,列出所有的过程:
    ------------------------------------------------------------------正式,查找电影,并列出CREATE procedure SearchFilm
                    (
                               @pagesize int,
                 @pageindex int,
                 @docount bit,
                               @FilmClass2  char(10),
                 @SearchChar  char(30)
    )
    as
                 set nocount on
                
    if(@docount=1)
                 select count(id) from Film_Info  where FilmName like '%"+@SearchChar+"%'    and  FilmClass=@FilmClass2
                
    else
                 begin
                 declare @indextable table(id int identity(1,1),nid int)
                 declare @PageLowerBound int
                 declare @PageUpperBound int
                 set @PageLowerBound=(@pageindex-1)*@pagesize
                 set @PageUpperBound=@PageLowerBound+@pagesize
                 set rowcount @PageUpperBound
                 insert into @indextable(nid) select id from Film_Info  order by ID desc
                 select *  from Film_Info  O,@indextable t where O.id=t.nid
                 and t.id>@PageLowerBound and t.id<=@PageUpperBound  and  FilmName like '%"+@SearchChar+"%'   and  FilmClass=@FilmClass2  order by t.id
                 end
                 set nocount off
    GO
      

  7.   

    参数化SQL,当用到 LIKE 关键字的时候,不要直接在 SQL 语句里写 % 和 ' 号 ,而是在 传入的参数上 加 % ,如:select count(id) from Film_Info  where FilmName like "@SearchChar "   and  FilmClass=@FilmClass2程序里, SearchChar = "%" + SearchChar + "%";
      

  8.   

    string sel="select count(id) from Film_Info  where FilmName like '%"+@SearchChar+"%'   and  FilmClass=@FilmClass2 ";
    我赞同
      

  9.   

    fanweiwei(哪来的为什么) :大哥,这样写真不行
      

  10.   

    最好使用全文搜索可以使用freetext
      

  11.   

    用存储过程:
    http://dev.csdn.net/develop/article/69/69396.shtm
      

  12.   

    现在用这个就行,但不会代替:select count(id) from Film_Info  where FilmName like '%'+'少林'+'%' and FilmClass=@FilmClass2这样就能查出所有的 影片名称中带“少林”的所有电影记录,
    但问题是:如何 用@SearchChar 来代替 这个 '少林',要是@SearchChar 写到
    select count(id) from Film_Info  where FilmName like '%'+'少林'+'%' and FilmClass=@FilmClass2 中,我不会写了
      

  13.   

    参数化SQL,当用到 LIKE 关键字的时候,不要直接在 SQL 语句里写 % 和 ' 号 ,而是在 传入的参数上 加 % ,如:select count(id) from Film_Info  where FilmName like "@SearchChar" and  FilmClass=@FilmClass2
     给 @SearchChar 传参数的时候,手动给参数 加上 % 号,注意不要 ' 号。
    楼主你认真看了没啊
      

  14.   

    我看过了,已经通过了。谢谢各位,不是用你们的方法。原来在like中,不能用char,应该用nvchar