SELECT * FROM NewsType WHERE ISNULL(ParentNewtypeCode,'')=ISNULL(@ParentNewtypeCode,ISNULL(ParentNewtypeCode,'')) ORDER BY ParentNewtypeCode,NewTypeCode DESC
为什么要ISNULL(ParentNewtypeCode,'')=ISNULL(@ParentNewtypeCode,ISNULL(ParentNewtypeCode,''))去判断
有什么好处呢
为什么要ISNULL(ParentNewtypeCode,'')=ISNULL(@ParentNewtypeCode,ISNULL(ParentNewtypeCode,''))去判断
有什么好处呢
我平常也是SELECT * FROM NewsType WHERE ParentNewtypeCode =@ParentNewtypeCode写的 可看到高手这样写 以为有更好的用途
我觉得是为了避免当ParentNewtypeCode 为空时, sql不正常执行
如果ParentNewtypeCode 是null,那么ISNULL(ParentNewtypeCode,'')
的值就是''
2:set @sqlwhere=@sqlwhere+N'AND ISNULL(TemplateName,'''')=ISNULL(@TemplateName,ISNULL(TemplateName,''''))' 那个N大家看到没 不知道什么意思
只是避免sql异常,并不一定是肯定有异常;
差不多吧
编程习惯防止为空???
因为null代表不确定,它即不会等于任务值也不会不等于任务值, 所以任何值用>= (或<>, =, <) 等运算符和null比较得到的结果都是false
这是正解了吧
刚才我试了一下,
select * from xxx where 字段1 = null是不好使的,可能是所谓的异常吧,
如果写成
select * from xxx where 字段1 is null才会查出想要的数据这样的话,isnull这个函数的确是能起到排除异常的作用
where 字段1 = null 这样写,即使字段1有null值,你依然查不到任何东西,
只能用is null才会查到为null的记录。所以说这里如果这样写:
SELECT * FROM NewsType WHERE ParentNewtypeCode =@ParentNewtypeCode
如果@ParentNewtypeCode这个参数为null,就变成了
SELECT * FROM NewsType WHERE ParentNewtypeCode = null
是查不到东西的只能写成
SELECT * FROM NewsType WHERE ParentNewtypeCode is null
不过这只是个人遇到的情况,也不一定是你那段代码的原意。
create table tempTable
(
tempColumn nvarchar(50)
)insert into tempTable select null
union all select 1
union all select 2select * from tempTabledeclare @var nvarchar(50)set @var=nullselect * from tempTable where tempColumn = @varselect * from tempTable where tempColumn is nullselect * from tempTable where isnull(tempColumn,'') = isnull(@var,'')set @var=1select * from tempTable where tempColumn = @vardrop table tempTable