开发者

ms sql use like statement result in if statement

开发者 https://www.devze.com 2022-12-25 22:02 出处:网络
declare @d 开发者_JAVA技巧varchar set @d = \'No filter\' if (@d like \'No filter\') BEGIN select \'matched\'
declare @d 开发者_JAVA技巧varchar
set @d = 'No filter'

if (@d like 'No filter')
  BEGIN
    select 'matched'
  end
else
  begin
    select 'not matched'
  end

the result of above is always not matched can anybody tell me why and how can I use the like or '=' result in my stored procedure. thanks


Change your declaration to

declare @d varchar(10)

Then

declare @d varchar(10)

set @d = 'No filter' 

if (@d LIKE 'No filter') 
  BEGIN 
    select 'matched' 
  end 
else 
  begin 
    select 'not matched' 
  end

will work.

Please remember that LIKE is used for pattern matching,

something like

DECLARE @Val VARCHAR(10)
SET @Val = 'foo bar'
if (@Val LIKE '%foo%') 
  BEGIN 
    select 'matched' 
  end 
else 
  begin 
    select 'not matched' 
  end

So in your case you might want to change the code to

declare @d varchar(10)
set @d = 'No filter' 

if (@d = 'No filter') 
  BEGIN 
    select 'matched' 
  end 
else 
  begin 
    select 'not matched' 
  end

with an "="


You need to declare d as varchar(100) not just varchar, otherwise it just becomes N

declare @d varchar
set @d = 'No filter'

Should be:

declare @d varchar(100)
set @d = 'No filter'

Also no need to use LIKE with this you can use =.


I find like operator very useful.

IF( @PLI_Component = '' + @ITemID + '' +'BD' OR @PLI_Component = '' + @ITemID + '' +'BD/1' OR @PLI_Component = '' + @ITemID + '' +'BD/2' OR @PLI_Component = '' + @ITemID + '' +'BD/3'
OR @PLI_Component = '' + @ITemID + '' +'BD/4'
OR @PLI_Component = '' + @ITemID + '' +'BD/5'

)

can be replaced

by only one line which is

@PLI_Component LIKE @ITemID +'BD'

Cheers

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号