日期:2014-05-18 浏览次数:20415 次
if object_id('tempdb..#t') is not null drop table #t create table #t(ID int identity(1,1) primary key,AppleName nvarchar(20),Weight decimal(6,2)) insert into #t(AppleName,Weight) select '苹果A',12.5 union all select '苹果B',12.5 union all select '苹果C',12.5 union all select '苹果D',12.5 union all select '苹果E',12.5 union all select '苹果F',12.5 union all select '苹果G',12.5 union all select '苹果H',12.5 union all select '苹果I',12.5 union all select '苹果J',12.5 union all select '苹果K',11.5 union all select '苹果L',12.5 select * from #t declare @searchApple int --待寻找的苹果ID declare @appleIsWeight bit --苹果是较重还是较轻 declare @errorMessage nvarchar(100) --分配的苹果与命题中的有差异产生的错误信息 set @errorMessage='' declare @weightFirst int, --第一次分组,0表示Group1和Group2等重,1表示Group1>Group2,2表示Group1<Group2 @weightSecond int, --第二次分组,0表示Group1和Group2等重,1表示Group1>Group2,2表示Group1<Group2 @weightThird int --第三次分组,0表示Group1和Group2等重,1表示Group1>Group2,2表示Group1<Group2 declare @weightA decimal(6,2),@weightB decimal(6,2) --进行第一次称重,Group1(1,2,3,4),Group2(4,5,6,7) select @weightA=sum(Weight) from #t where ID > 0 and ID <= 4 select @weightB=sum(Weight) from #t where ID > 4 and ID <= 8 if @weightA=@weightB set @weightFirst=0 else if @weightA>@weightB set @weightFirst=1 else set @weightFirst=2 --第二次称重 ----如果第一次天枰平衡,则在剩下的两个里面筛选(比较方式:拿一个和1-8中任意一个比较) if @weightFirst=0 begin select @weightA=Weight from #t where ID = 1 select @weightB=Weight from #t where ID = 11 if @weightA>@weightB begin s