if  @item_desc<>'' and @item_desc<>'*ALL' 
set @where = @where + ' and a.item_desc like'''+@item_desc+'%''' 
if  @wh_loc<>'' and @wh_loc<>'*ALL'
set @where = @where + ' and a.src_wh ='''+@wh_loc+''''
if  @type<>'' and @type<>'*ALL'
set @where = @where + ' and a.type like''%'+@type+'%'''
if  @prod<>'' and @prod<>'*ALL'
set @where = @where + ' and a.prod like'''+@prod+'%'''
if  @vnd_id<>'' and @vnd_id<>'*ALL'
set @where = @where + ' and a.item_code in (select item_code from items where coalesce(vnd_id,'''')='''+@vnd_id+''' )' if  @sort<>'' and @sort<>'*ALL'
 set @where = @where + ' and a.sort ='''+@sort+ ''''
if  @cust_id<>'' and @cust_id<>'*ALL'
 set @where = @where + ' and a.cust_id='''+@cust_id+ ''''
 if  @cust_region<>'' and @cust_region<>'*ALL'
set @where=@where+' and a.cust_id  in (select  cust_id from customer where  coalesce(cust_region,'''')='''+@cust_region+''') '各位大仙,妖怪……请求一个多年缠困我的SQL问题!!那么引号是怎么用的,看不懂,求指教……深谢了,深谢了……

解决方案 »

  1.   


    一般会用双单引号来转义用来存储带有单引号的内容,这里介绍另外一种转换方法 char(39) 也可以代表单引号。一 update A set name='O''neal' where...二 update A set name='O' + char(39) + 'neal' where.... 上边2个sql语句执行的效果是一样的。 ---------------------------------------------------------------------------------------------------------------------------------------sql server有两个转义符: '默认情况下, '是字符串的边界符(半角的单引号), 如果在字符串中包含', 则必须使用两个',第1个'就是转义符 另一个转义符是" (双引号,半角)当SET QUOTED_IDENTIFIER OFF时, "是字符串边界符, 字符串中的"必须用两个"表示。 vb: ""<=> "sql server 2000: '''<=> 'eg: declare @SearchType nvarchar(50) declare @SearchString nvarchar(255) declare @SearchKey nvarchar(50) declare @SearchSql nvarchar(2000) set @SearchType = '2' set @SearchKey = 'd' set @SearchString = CASE @SearchType when '1' then '1 = 1' when '2' then 'p.ProjectName like '''+ '%' + @searchkey + '%' + '''' when '3' then 'p.ProjectCity like '''+ '%'+ @searchkey + '%' +'''' when '4' then 'c.CateName like '''+ '%' + @searchkey + '%' + '''' when '4' then 'p.ProjectManager like '''+ '%' + @searchkey + '%' +'''' END set @SearchSql = N' SELECT p.*,datename(year,ProjectPostTime)+ ' + '''-''' + '+ datename(month,ProjectPostTime)+ '+ '''-''' + '+ datename(day,ProjectPostTime)' + 'as PostTime, m.EmpName,c.CateName FROM proProject As p ,mrBaseInf As m ,proCate c WHERE p.EmpID = m.EmpID and p.CateID = c.CateID and '+ @SearchString print(@SearchSql) exec(@SearchSql) SET QUOTED_IDENTIFIER OFF 是 SQL-92 设置语句,使 SQL Server 2000/2005 遵从 SQL-92 规则。 当 SET QUOTED_IDENTIFIER 为 ON 时,标识符可以由双引号分隔,而文字必须由单引号分隔。当 SET QUOTED_IDENTIFIER 为 OFF 时,标识符不可加引号,且必须符合所有 Transact-SQL 标识符规则。 SQL-92 标准要求在对空值进行等于 (=) 或不等于 (<>) 比较时取值为 FALSE。当 SET ANSI_NULLS 为 ON 时,即使 column_name 中包含空值,使用 WHERE column_name = NULL 的 SELECT 语句仍返回零行。即使 column_name 中包含非空值,使用 WHERE column_name <> NULL 的 SELECT 语句仍会返回零行。 当 SET ANSI_NULLS 为 OFF 时,等于 (=) 和不等于 (<>) 比较运算符不遵从 SQL-92 标准。使用 WHERE column_name = NULL 的 SELECT 语句返回 column_name 中包含空值的行。使用 WHERE column_name <> NULL 的 SELECT 语句返回列中包含非空值的行。此外,使用 WHERE column_name <> XYZ_value 的 SELECT 语句返回所有不为 XYZ_value 也不为 NULL 的行。一、搜索通配符字符的说明
    可以搜索通配符字符。有两种方法可指定平常用作通配符的字符:使用 ESCAPE 关键字定义转义符。在模式中,当转义符置于通配符之前时,该通配符就解释为普通字符。例如,要搜索在任意位置包含字符串 5% 的字符串,请使用:
    WHERE ColumnA LIKE '%5/%%' ESCAPE '/'在上述 LIKE 子句中,前导和结尾百分号 (%) 解释为通配符,而斜杠 (/) 之后的百分号解释为字符 %。在方括号 ([ ]) 中只包含通配符本身。要搜索破折号 (-) 而不是用它指定搜索范围,请将破折号指定为方括号内的第一个字符:
    WHERE ColumnA LIKE '9[-]5'下表显示了括在方括号内的通配符的用法。符号 含义
    LIKE '5[%]' 5%
    LIKE '5%' 5 后跟 0 个或更多字符的字符串
    LIKE '[_]n' _n
    LIKE '_n' an, in, on (and so on)
    LIKE '[a-cdf]' a, b, c, d, or f
    LIKE '[-acdf]' -, a, c, d, or f
    LIKE '[ [ ]' [
    LIKE ']' ]
    二、实例说明:在表PersonalMember中查找strloginname字段中含有"["的记录。
    可用三条语句:
    1、
    select strloginname,* from PersonalMember where strloginname like '%\[%' escape '\'2、(说明"\"与"/"均可与escape关键字结合作为转义符)
    select strloginname,* from PersonalMember where strloginname like '%/[%' escape '/'3、
    select strloginname,* from dbo.PersonalMember where charindex('[',strloginname)>0文章转载自网管之家:http://www.bitscn.com/plus/view.php?aid=97312
      

  2.   

    细心点就好呢,在SQL Server 中 '' 当于实际’ 
      

  3.   

    用print @where 来调试就方便多了