帮忙看一下sql语句再sql 2000中是怎么识别的?

Hai1982 2005-05-25 11:05:45
declare @mode varchar(10)
set @mode ='1234'
select * from whf where customerid like '%'+ @mode +'%'

select * from whf where customerid like ''%'+ @mode +'%''
的区别


在select * from whf where customerid like '%'+ @mode +'%'中怎莫再@mode两边加上' '
...全文
111 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Hai1982 2005-05-26
  • 打赏
  • 举报
回复
不明白,死记吧
谢谢
zhangzs8896 2005-05-26
  • 打赏
  • 举报
回复
select * from whf where customerid like '%'+ @mode +'%'
'%'不是一个字符吗?怎么会还有一个'号呢?
--
这样,你可以直接使用'%1234%'
但是因为你的sql中使用了变量@mode
所以必须'%'+@mode+'%',传进参数值后相当与'%1234%'.
这样说不知道明白了吗?
你可以分别执行一下,看一下结果就明白了。
zhangzs8896 2005-05-26
  • 打赏
  • 举报
回复
declare @mode varchar(10)
set @mode ='1234'

select * from whf where customerid like '%'+ @mode +'%'
执行的是:
select * from whf where customerid like '%1234%' ----注意

select * from whf where customerid like '''%'+ @mode +'%'''
执行的是:
select * from whf where customerid like ''%1234%'' ---注意

两个是不一样的,前着的串是 %1234% 后者是'%1234%',也就是后着串中包含''
其实记住一点,在串里两个单引号相当于一个单引号
Hai1982 2005-05-26
  • 打赏
  • 举报
回复
有点晕,
select * from whf where customerid like '%'+ @mode +'%'
'%'不是一个字符吗?怎么会还有一个'号呢?
Hai1982 2005-05-26
  • 打赏
  • 举报
回复
哦,是报错了。
也就是说
select * from whf where customerid like '%'+ @mode +'%'

select * from whf where customerid like '''%'+ @mode +'%'''

是一样的都是执行的select * from whf where customerid like '%1234%'
zhangzs8896 2005-05-26
  • 打赏
  • 举报
回复
declare @mode varchar(10)
set @mode ='1234'

select * from whf where customerid like '%'+ @mode +'%'
执行的是:
select * from whf where customerid like '%1234%'

select * from whf where customerid like '''%'+ @mode +'%'''
执行的是:
select * from whf where customerid like ''%1234%''

而楼主的:
select * from whf where customerid like ''%'+ @mode +'%''
执行的是:
select * from whf where customerid like %1234%
Hai1982 2005-05-26
  • 打赏
  • 举报
回复
select * from whf where customerid like ''%'+ @mode +'%''
在sql 2000中并没有报错呀?那它执行的是什么呀?
zjcxc 元老 2005-05-26
  • 打赏
  • 举报
回复
--放在字符串中的',一律用两个''表示,所以应该修改如下
select * from whf where customerid like '''%'+ @mode +'%'''
Hai1982 2005-05-26
  • 打赏
  • 举报
回复
分数不够可以在加,急求答案。
zjcxc 元老 2005-05-26
  • 打赏
  • 举报
回复
declare @mode varchar(10)
set @mode ='1234'

select * from whf where customerid like '%'+ @mode +'%'
执行的是:
select * from whf where customerid like '%1234%'



select * from whf where customerid like ''%'+ @mode +'%''
这个语法上是错的
所以两句没有执行上的可比较性

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧