0

在我的存储过程中,我现在尝试将 FEE 添加到我的销售中。

  • 如果 item_price > 69 它应该有 6 in fee
  • 如果 item_price <= 69 它应该有 3 in fee
  • 如果 item_price <= 19 它应该有费用 1

我尝试将它添加到我的 SELECT 语句中,但我不知道如何正确插入它。

   AS 'Income inc VAT, ex Discount',
   case when item_price > 69 then 6
   case when item_price <= 69 then 3
   else 1
   AS 'fee'

为了举例说明我如何尝试实现它,我添加了一些选择语句......

解决方案:在我的选择语句中,我根据我得到的很好的答案添加了这段代码,它起作用了:

       case when item_price > 69 then 6 
       when item_price <= 69 then 3 
       else 1
       end
       AS 'Fee'
4

1 回答 1

1

您应该使用 case 语句:

凭记忆写:

insert into fee (fee_value)
select 
  case when item_price > 69 then 6
  case when item_price <= 69 then 3
  else 1
end

等等...

或者,也许您想使用变量:

declare @fee int

if @item_price > 69 
 set @fee = 6

...

insert into fee(fee_value) 
values (@fee)

或者另一种方式:

declare @item_price int  = 12

declare @fee int

SELECT
  @fee = case 
           when @item_price > 69 then 6
           when @item_price between 13 and 69 then 2
           else 1
         end

select @fee   
于 2014-04-28T07:26:38.813 回答