日期:2014-05-18  浏览次数:20495 次

sql语句用 case end 判断正负后给变量赋值问题
declare @AVAILABLE_STOCK int
select
 @AVAILABLE_STOCK=
  (case 
  when AVAILABLE_STOCK<0 
  then 0
  else AVAILABLE_STOCK
  end)
  as [库存] from Table
请问我想用case end 判断数据库字段AVAILABLE_STOCK的正负结果赋值给变量@AVAILABLE_STOCK这句话这么改?请教!!!

------解决方案--------------------
SQL code

declare @a int
declare @b int
set @a=-3
select @b=case when @a<0 then 0 else @a end
select @b as value

/*
value
0
*/

--你这样不行吗??

------解决方案--------------------
SQL code

if object_id('[lsxf1988_Table]') is not null drop table [lsxf1988_Table]
create table [lsxf1988_Table]([AVAILABLE_STOCK] int)
insert [lsxf1988_Table]
select -1 union all
select 9 union all
select 8

declare @AVAILABLE_STOCK int
set  @AVAILABLE_STOCK=( select top 1 
case when AVAILABLE_STOCK<0 then 0
else AVAILABLE_STOCK end from [lsxf1988_Table])

select @AVAILABLE_STOCK as [库存]
/*
库存
-----------
0
*/