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

如何用SQL语句查询后,返回不存在的值,并用一个0代替
如何用SQL语句查询后,返回不存在的值,并用0代替
select isNull(PremiumOld,0) as PremiumOld from view_Prodect 
where Material='304HC' and  [Name]='GB818' and Specification='M1.6*18'
如果这个语句查询没有结果。则显示 列名 PremiumOld 显示为 0
------解决方案--------------------
select isNull(PremiumOld,0) as PremiumOld from view_Prodect 
 where Material='304HC' and  [Name]='GB818' and Specification='M1.6*18'
union all
select 0 as PremiumOld from (select 0 as A) as TB where not exists(select 1 from view_Prodect 
 where Material='304HC' and  [Name]='GB818' and Specification='M1.6*18') 
------解决方案--------------------
select case when isNull(PremiumOld,'')='' then 0 Else PremiumOld End as PremiumOld from view_Prodect 
where Material='304HC' and  [Name]='GB818' and Specification='M1.6*18'