| 订阅 | 在线投稿
分享
 
 
 

解析Microsoft Sql Server中的like语句

2008-12-13 08:23:53 编辑來源:互联网 国际版 评论
 
 
本文为【解析Microsoft Sql Server中的like语句】的汉字拼音对照版显示拼音
  LIKE,quedinggeidingdezifuchuanshifouyuzhidingdemoshipipeimoshikeyibaohanchangguizifuhetongpeifuzifumoshipipeiguochengzhongchangguizifubixuyuzifuchuanzhongzhidingdezifuwanquanpipeiranerke使shiyongzifuchuanderenyipianduanpipeitongpeifuyu使shiyong = he != zifuchuanbijiaoyunsuanfuxiangbi使shiyongtongpeifuke使shi LIKE yunsuanfugengjialinghuo

  ruguorenheshenshudoubushuyuzifuchuanshujuleixingMicrosoft® SQL Server™ huijiangqizhuanhuanchengzifuchuanshujuleixingruguokeneng

  yufa view plaincopy to clipboardprint?

  match_expression [ NOT ] LIKE pattern [ ESCAPE escape_character ]

  match_expression [ NOT ] LIKE pattern [ ESCAPE escape_character ]shenshu

  match_expression

  renhezifuchuanshujuleixingdeyouxiao SQL Server biaodashi

  pattern

  match_expression zhongdesousuomoshikeyibaohanxialieyouxiao SQL Server tongpeifu

  tongpeifu miaoshushili

  % baohanlinggehuogengduozifuderenyizifuchuan WHERE title LIKE '%computer%' jiangchazhaochuyushumingrenyiweizhidebaohandanci computer desuoyoushuming

  _xiahua线xian renhedangezifu WHERE au_fname LIKE '_ean' jiangchazhaoyi ean jieyidesuoyou 4 gezimudemingziDeanSean deng

  [ ] zhidingfanwei ([a-f]) huojihe ([abcdef]) zhongderenhedangezifu WHERE au_lname LIKE '[C-P]arsen' jiangchazhaoyiarsen jieyiqieyijieyu C yu P zhijianderenhedangezifukaishidezuozhexingshiliruCarsenLarsenKarsen deng

  [^] bushuyuzhidingfanwei ([a-f]) huojihe ([abcdef]) derenhedangezifu WHERE au_lname LIKE 'de[^l]%' jiangchazhaoyi de kaishiqieqihoudezimubuwei l desuoyouzuozhedexingshi

  escape_character

  zifuchuanshujuleixingfenleizhongdesuoyoushujuleixingderenheyouxiao SQL Server biaodashiescape_character meiyoumorenzhiqiebixujinbaohanyigezifu

  jieguoleixing

  Boolean

  jieguozhi

  ruguo match_expression pipeizhidingmoshiLIKE jiangfanhui TRUE

  zhushi

  dang使shiyong LIKE jinxingzifuchuanbijiaoshimoshizifuchuanzhongdesuoyouzifudouyouyiyibaokuoqishihuoyisuikonggeruguochaxunzhongdebijiaoyaofanhuibaohan"abc "abc houyouyigekonggedesuoyouxingzejiangbuhuifanhuibaohan"abc"abc houmeiyoukonggedeliesuozaixingdanshikeyihuluemoshisuoyaopipeidebiaodashizhongdeyisuikonggeruguochaxunzhongdebijiaoyaofanhuibaohan"abc"abc houmeiyoukonggedesuoyouxingzejiangfanhuiyi"abc"kaishiqiejuyoulinggehuoduogeyisuikonggedesuoyouxing

  bentielaiziZDNetChinazhongwenshequ http://bbs.zdnet.com.cn ,bentiedizhihttp://bbs.zdnet.com.cn/viewthread.php?tid=94371

  youyushujucunchufangshideyuanyin使shiyongbaohan char he varchar shujumoshidezifuchuanbijiaokenengwufatongguo LIKE bijiaolejiemeizhongshujuleixingdecunchufangshiyijidaozhi LIKE bijiaoshibaideyuanyinshifenchongyaoxiamiandeshilijiangjubu char bianliangchuandigeicunchuguochengranhou使shiyongmoshipipeichazhaomougezuozhedesuoyouzhuzuozaiciguochengzhongyongyigexingshizuoweibianliangchuandi view plaincopy to clipboardprint?

  CREATE PROCEDURE find_books @AU_LNAME char(20)

  

  AS

  

  SELECT @AU_LNAME = RTRIM(@AU_LNAME) + '%'

  

  SELECT t.title_id, t.title

  

  FROM authors a, titleauthor ta, titles t

  

  WHERE a.au_id = ta.au_id AND ta.title_id = t.title_id

  

  AND a.au_lname LIKE @AU_LNAME

  CREATE PROCEDURE find_books @AU_LNAME char(20)

  AS

  SELECT @AU_LNAME = RTRIM(@AU_LNAME) + '%'

  SELECT t.title_id, t.title

  FROM authors a, titleauthor ta, titles t

  WHERE a.au_id = ta.au_id AND ta.title_id = t.title_id

  AND a.au_lname LIKE @AU_LNAMEdangmingzizhongbaohandezifushuxiaoyu 20 shichar bianliang (@AU_LNAME) jiangbaohanyisuikonggezhedaozhi find_books guochengzhongmeiyouxingfanhuiyouyu au_lname liewei varchar leixingsuoyimeiyouyisuikonggeyinweiyisuikonggeshiyouyiyidesuoyiciguochengshibai

  danxiamiandeshilishichenggongdeyinweiyisuikonggemeiyoubeitianjiadao varchar bianliangzhong view plaincopy to clipboardprint?

  USE pubs

  

  GO

  

  CREATE PROCEDURE find_books2 @au_lname varchar(20)

  

  AS

  

  SELECT t.title_id, t.title

  

  FROM authors a, titleauthor ta, titles t

  

  WHERE a.au_id = ta.au_id AND ta.title_id = t.title_id

  

  AND a.au_lname LIKE @au_lname + '%'

  

  EXEC find_books2 'ring'

  USE pubs

  GO

  CREATE PROCEDURE find_books2 @au_lname varchar(20)

  AS

  SELECT t.title_id, t.title

  FROM authors a, titleauthor ta, titles t

  WHERE a.au_id = ta.au_id AND ta.title_id = t.title_id

  AND a.au_lname LIKE @au_lname + '%'

  EXEC find_books2 'ring'xiamianshijieguoji

  title_id title

  -------- -----------------

  MC3021 The Gourmet Microwave

  PS2091 Is Anger the Enemy?

  PS2091 Is Anger the Enemy?

  PS2106 Life Without Fear

  (4 row(s) affected)

  youyushujucunchufangshideyuanyin使shiyongbaohan char he varchar shujumoshidezifuchuanbijiaokenengwufatongguo LIKE bijiaolejiemeizhongshujuleixingdecunchufangshiyijidaozhi LIKE bijiaoshibaideyuanyinshifenchongyaoxiamiandeshilijiangjubu char bianliangchuandigeicunchuguochengranhou使shiyongmoshipipeichazhaomougezuozhedesuoyouzhuzuozaiciguochengzhongyongyigexingshizuoweibianliangchuandi view plaincopy to clipboardprint?

  CREATE PROCEDURE find_books @AU_LNAME char(20)

  

  AS

  

  SELECT @AU_LNAME = RTRIM(@AU_LNAME) + '%'

  

  SELECT t.title_id, t.title

  

  FROM authors a, titleauthor ta, titles t

  

  WHERE a.au_id = ta.au_id AND ta.title_id = t.title_id

  

  AND a.au_lname LIKE @AU_LNAME

  CREATE PROCEDURE find_books @AU_LNAME char(20)

  AS

  SELECT @AU_LNAME = RTRIM(@AU_LNAME) + '%'

  SELECT t.title_id, t.title

  FROM authors a, titleauthor ta, titles t

  WHERE a.au_id = ta.au_id AND ta.title_id = t.title_id

  AND a.au_lname LIKE @AU_LNAMEdangmingzizhongbaohandezifushuxiaoyu 20 shichar bianliang (@AU_LNAME) jiangbaohanyisuikonggezhedaozhi find_books guochengzhongmeiyouxingfanhuiyouyu au_lname liewei varchar leixingsuoyimeiyouyisuikonggeyinweiyisuikonggeshiyouyiyidesuoyiciguochengshibai

  danxiamiandeshilishichenggongdeyinweiyisuikonggemeiyoubeitianjiadao varchar bianliangzhong view plaincopy to clipboardprint?

  USE pubs

  

  GO

  

  CREATE PROCEDURE find_books2 @au_lname varchar(20)

  

  AS

  

  SELECT t.title_id, t.title

  

  FROM authors a, titleauthor ta, titles t

  

  WHERE a.au_id = ta.au_id AND ta.title_id = t.title_id

  

  AND a.au_lname LIKE @au_lname + '%'

  

  EXEC find_books2 'ring'

  USE pubs

  GO

  CREATE PROCEDURE find_books2 @au_lname varchar(20)

  AS

  SELECT t.title_id, t.title

  FROM authors a, titleauthor ta, titles t

  WHERE a.au_id = ta.au_id AND ta.title_id = t.title_id

  AND a.au_lname LIKE @au_lname + '%'

  EXEC find_books2 'ring'xiamianshijieguoji

  title_id title

  -------- -----------------

  MC3021 The Gourmet Microwave

  PS2091 Is Anger the Enemy?

  PS2091 Is Anger the Enemy?

  PS2106 Life Without Fear

  (4 row(s) affected)原文
 
 
LIKE,确定给定的字符串是否与指定的模式匹配。模式可以包含常规字符和通配符字符。模式匹配过程中,常规字符必须与字符串中指定的字符完全匹配。然而,可使用字符串的任意片段匹配通配符。与使用 = 和 != 字符串比较运算符相比,使用通配符可使 LIKE 运算符更加灵活。 如果任何参数都不属于字符串数据类型,Microsoft® SQL Server™ 会将其转换成字符串数据类型(如果可能)。 语法 view plaincopy to clipboardprint? match_expression [ NOT ] LIKE pattern [ ESCAPE escape_character ] match_expression [ NOT ] LIKE pattern [ ESCAPE escape_character ]参数 match_expression 任何字符串数据类型的有效 SQL Server 表达式。 pattern match_expression 中的搜索模式,可以包含下列有效 SQL Server 通配符。 通配符 描述示例 % 包含零个或更多字符的任意字符串。 WHERE title LIKE '%computer%' 将查找处于书名任意位置的包含单词 computer 的所有书名。 _(下划线) 任何单个字符。 WHERE au_fname LIKE '_ean' 将查找以 ean 结尾的所有 4 个字母的名字(Dean、Sean 等)。 [ ] 指定范围 ([a-f]) 或集合 ([abcdef]) 中的任何单个字符。 WHERE au_lname LIKE '[C-P]arsen' 将查找以arsen 结尾且以介于 C 与 P 之间的任何单个字符开始的作者姓氏,例如,Carsen、Larsen、Karsen 等。 [^] 不属于指定范围 ([a-f]) 或集合 ([abcdef]) 的任何单个字符。 WHERE au_lname LIKE 'de[^l]%' 将查找以 de 开始且其后的字母不为 l 的所有作者的姓氏。 escape_character 字符串数据类型分类中的所有数据类型的任何有效 SQL Server 表达式。escape_character 没有默认值,且必须仅包含一个字符。 结果类型 Boolean 结果值 如果 match_expression 匹配指定模式,LIKE 将返回 TRUE。 注释 当使用 LIKE 进行字符串比较时,模式字符串中的所有字符都有意义,包括起始或尾随空格。如果查询中的比较要返回包含"abc "(abc 后有一个空格)的所有行,则将不会返回包含"abc"(abc 后没有空格)的列所在行。但是可以忽略模式所要匹配的表达式中的尾随空格。如果查询中的比较要返回包含"abc"(abc 后没有空格)的所有行,则将返回以"abc"开始且具有零个或多个尾随空格的所有行。 本贴来自ZDNetChina中文社区 [url=http://bbs.zdnet.com.cn]http://bbs.zdnet.com.cn[/url] ,本贴地址:[url=http://bbs.zdnet.com.cn/viewthread.php?tid=94371]http://bbs.zdnet.com.cn/viewthread.php?tid=94371[/url] 由于数据存储方式的原因,使用包含 char 和 varchar 数据模式的字符串比较可能无法通过 LIKE 比较。了解每种数据类型的存储方式以及导致 LIKE 比较失败的原因十分重要。下面的示例将局部 char 变量传递给存储过程,然后使用模式匹配查找某个作者的所有著作。在此过程中,用一个性氏作为变量传递。 view plaincopy to clipboardprint? CREATE PROCEDURE find_books @AU_LNAME char(20) AS SELECT @AU_LNAME = RTRIM(@AU_LNAME) + '%' SELECT t.title_id, t.title FROM authors a, titleauthor ta, titles t WHERE a.au_id = ta.au_id AND ta.title_id = t.title_id AND a.au_lname LIKE @AU_LNAME CREATE PROCEDURE find_books @AU_LNAME char(20) AS SELECT @AU_LNAME = RTRIM(@AU_LNAME) + '%' SELECT t.title_id, t.title FROM authors a, titleauthor ta, titles t WHERE a.au_id = ta.au_id AND ta.title_id = t.title_id AND a.au_lname LIKE @AU_LNAME当名字中包含的字符数小于 20 时,char 变量 (@AU_LNAME) 将包含尾随空格,这导致 find_books 过程中没有行返回。由于 au_lname 列为 varchar 类型,所以没有尾随空格。因为尾随空格是有意义的,所以此过程失败。 但下面的示例是成功的,因为尾随空格没有被添加到 varchar 变量中: view plaincopy to clipboardprint? USE pubs GO CREATE PROCEDURE find_books2 @au_lname varchar(20) AS SELECT t.title_id, t.title FROM authors a, titleauthor ta, titles t WHERE a.au_id = ta.au_id AND ta.title_id = t.title_id AND a.au_lname LIKE @au_lname + '%' EXEC find_books2 'ring' USE pubs GO CREATE PROCEDURE find_books2 @au_lname varchar(20) AS SELECT t.title_id, t.title FROM authors a, titleauthor ta, titles t WHERE a.au_id = ta.au_id AND ta.title_id = t.title_id AND a.au_lname LIKE @au_lname + '%' EXEC find_books2 'ring'下面是结果集: title_id title -------- ----------------- MC3021 The Gourmet Microwave PS2091 Is Anger the Enemy? PS2091 Is Anger the Enemy? PS2106 Life Without Fear (4 row(s) affected) 由于数据存储方式的原因,使用包含 char 和 varchar 数据模式的字符串比较可能无法通过 LIKE 比较。了解每种数据类型的存储方式以及导致 LIKE 比较失败的原因十分重要。下面的示例将局部 char 变量传递给存储过程,然后使用模式匹配查找某个作者的所有著作。在此过程中,用一个性氏作为变量传递。 view plaincopy to clipboardprint? CREATE PROCEDURE find_books @AU_LNAME char(20) AS SELECT @AU_LNAME = RTRIM(@AU_LNAME) + '%' SELECT t.title_id, t.title FROM authors a, titleauthor ta, titles t WHERE a.au_id = ta.au_id AND ta.title_id = t.title_id AND a.au_lname LIKE @AU_LNAME CREATE PROCEDURE find_books @AU_LNAME char(20) AS SELECT @AU_LNAME = RTRIM(@AU_LNAME) + '%' SELECT t.title_id, t.title FROM authors a, titleauthor ta, titles t WHERE a.au_id = ta.au_id AND ta.title_id = t.title_id AND a.au_lname LIKE @AU_LNAME当名字中包含的字符数小于 20 时,char 变量 (@AU_LNAME) 将包含尾随空格,这导致 find_books 过程中没有行返回。由于 au_lname 列为 varchar 类型,所以没有尾随空格。因为尾随空格是有意义的,所以此过程失败。 但下面的示例是成功的,因为尾随空格没有被添加到 varchar 变量中: view plaincopy to clipboardprint? USE pubs GO CREATE PROCEDURE find_books2 @au_lname varchar(20) AS SELECT t.title_id, t.title FROM authors a, titleauthor ta, titles t WHERE a.au_id = ta.au_id AND ta.title_id = t.title_id AND a.au_lname LIKE @au_lname + '%' EXEC find_books2 'ring' USE pubs GO CREATE PROCEDURE find_books2 @au_lname varchar(20) AS SELECT t.title_id, t.title FROM authors a, titleauthor ta, titles t WHERE a.au_id = ta.au_id AND ta.title_id = t.title_id AND a.au_lname LIKE @au_lname + '%' EXEC find_books2 'ring'下面是结果集: title_id title -------- ----------------- MC3021 The Gourmet Microwave PS2091 Is Anger the Enemy? PS2091 Is Anger the Enemy? PS2106 Life Without Fear (4 row(s) affected)
󰈣󰈤
 
 
>>返回首页<<
 为你推荐
 
 
 
 转载本文
 UBB代码 HTML代码
复制到剪贴板...
 
 
 热帖排行
 
 
王朝网络微信公众号
微信扫码关注本站公众号wangchaonetcn
 
  免责声明:本文仅代表作者个人观点,与王朝网络无关。王朝网络登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
©2005- 王朝网络 版权所有