AVAILABLE_STOCK有几条记录呀?多对一的话,你要怎么赋值?

解决方案 »

  1.   


    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
    */--你这样不行吗??
      

  2.   


    declare @AVAILABLE_STOCK int
    select
     @AVAILABLE_STOCK=
      (case 
      when AVAILABLE_STOCK<0 
      then 0
      else AVAILABLE_STOCK
      end) from Table where ...  --要限定一条记录
      

  3.   


    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 8declare @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
    */多个值的时候可以使用top 1赋值的同时不能起别名。
      

  4.   

     @AVAILABLE_STOCK=(select top 1 
                       case when AVAILABLE_STOCK<0 then 0
                       else AVAILABLE_STOCK end from TABLE)
           as [库存]
    消息 156,级别 15,状态 1,第 24 行
    关键字 'as' 附近有语法错误。报这错?
      

  5.   

    去掉了 别名 报这 
    消息 141,级别 15,状态 1,第 3 行
    向变量赋值的 SELECT 语句不能与数据检索操作结合使用。
      

  6.   

    你的是SQL SERVER数据库吗?
      

  7.   


    if object_id('[lsxf1988_Table]') is not null drop table [lsxf1988_Table]
    create table [lsxf1988_Table]([AVAILABLE_STOCK] int)
    insert [lsxf1988_Table] select -9 declare @AVAILABLE_STOCK int
    select
     @AVAILABLE_STOCK=
      (case  
      when AVAILABLE_STOCK<0  
      then 0
      else AVAILABLE_STOCK
      end)
     from [lsxf1988_Table]select @AVAILABLE_STOCK as 库存
    这样是没有问题的。
      

  8.   


    if object_id('[lsxf1988_Table]') is not null drop table [lsxf1988_Table]
    create table [lsxf1988_Table]([AVAILABLE_STOCK] int)
    insert [lsxf1988_Table] select -9 declare @AVAILABLE_STOCK int
    select @AVAILABLE_STOCK,
     @AVAILABLE_STOCK=
      (case  
      when AVAILABLE_STOCK<0  
      then 0
      else AVAILABLE_STOCK
      end)
     from [lsxf1988_Table]select @AVAILABLE_STOCK as 库存
    --A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations.
    这样就会报错了,赋值的同时不要直接查询
      

  9.   

    declare @AVAILABLE_STOCK int
    select
     @AVAILABLE_STOCK=
      (case  
      when AVAILABLE_STOCK<0  
      then 0
      else AVAILABLE_STOCK
      end)
      as [库存] from Table
    看这个语句意思是,如果为负数就赋值为0,否则就是原值
    这样的话
    用绝对值函数试试,(AVAILABLE_STOCK + abs(AVAILABLE_STOCK))/2赋值给@AVAILABLE_STOCK
      

  10.   

    declare @AVAILABLE_STOCK int
    select
     @AVAILABLE_STOCK= @AVAILABLE_STOCK+
           (case 
              when AVAILABLE_STOCK<0 
              then 0
              else AVAILABLE_STOCK
           end)
           as [库存] from Table如果你查询的类型不一致,需要自己转换类型