because ' is also used to delimit literal strings, if you need to include ' inside the string, you need to use '', so in your case, @where will contain (assume @name is 'abc')where name like '%abc%'

解决方案 »

  1.   

    declare          @Where      varchar(300)  
                         select  @Where  =  '  where  '  
    select  @Where  =  @Where  +  'name  like  ''%'  +  @name+    '%'''  
    在sql的查選環境中加上
    print @where 去看看結果就明白了.
      

  2.   

    两个'可以转换成字符串里的一个',在SQL的WHERE子句里要用到字符串,匹配的是字符串的值,需要以'来作为分隔符,'name  like  ''%'  +  @name+    '%''' 生成的字符串是name like '%name变量内容%'