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

关于变量的求助
有表ZK

数量 折扣
2 10
4 15
6 20

当用户输入1时,就不带出折扣,当用户输入2时就带出折扣10.也就是说满足数量条件就带出相应的折扣

但是这个数量条件和折扣都是是不定的,经常会去变,所以我想把数量和折扣也做成变量,求高手指点

------解决方案--------------------
SQL code
if object_id('[ZK]') is not null drop table [ZK]
go
create table [ZK] (数量 int,折扣 int)
insert into [ZK]
select 2,10 union all
select 4,15 union all
select 6,20

select * from [ZK]


declare @i int 
set @i = 2

select 数量,折扣 from ZK where 数量 = @i

/*
数量    折扣
2    10*/

------解决方案--------------------
SQL code
declare @i int 
set @i=3
if @i<2 
   select 1[数量],0[折扣]
else if @i<4
  select @i [数量],折扣 from zk where 数量=2
else if @i<6
  select @i [数量],折扣 from zk where 数量=4
else 
  select @i [数量],折扣 from zk where 数量=6

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

declare @qty int,@discount int
select @qty=1;
select @discount=isnull((select top 1 折扣 from 你的表
    where t.数量<=@qty order by 数量 desc),0);
select @qty 数量, @discount 折扣;