日期:2014-05-17  浏览次数:20459 次

一个郁闷的问题, 0=''
某普通查询,参数为INT,我传0进去
判断为''..

 AND (@Edit_State IS NULL OR @Edit_State ='' OR Edit_State=@Edit_State)

print @Edit_State

if(@Edit_State='')
  print 'kong'
  
print @Edit_State


输出:
0
kong
0

何解?

------解决方案--------------------
不建议拿int型变量跟字符型做比较,
可以写为 @Edit_State=0 或 @Edit_State<>0
------解决方案--------------------

declare @Edit_State int
set @Edit_State=0

print @Edit_State
if(@Edit_State='')
  print 'kong'
print @Edit_State

/*
0
kong
0
*/

declare @Edit_State varchar(10)
set @Edit_State=0

print @Edit_State
if(@Edit_State='')
  print 'kong'
print @Edit_State

declare @Edit_State varchar(10)
set @Edit_State=0

print @Edit_State
if(@Edit_State='')
  print 'kong'
print @Edit_State


/*
0
0
*/

说明传送0参数时,与字符串比较,默认是空字符串

------解决方案--------------------
引用:
ALTER PROC [dbo].[Loan_GetApplyList]
(
   @Edit_State tinyint
)
AS
BEGIN


print @Edit_State

if(@Edit_State='')
  print 'kong'
  
print @Edit_State

  select case Edit_State when 1 then '编辑' when 0 then '作废' when 2 then '完成提交' end as Edit_State
  from vQueryList 
  where hNo=@HRNO 
  AND (@Edit_State IS NULL OR @Edit_State ='' OR Edit_State=@Edit_State)
  ORDER BY ApplyDate DESC
END

你用的是INT型,但是if(@Edit_State='')的时候会将INT型转换为字符型的Ascii码,比如CHAR(0)你看看是不是空。