0

我有一个表,在 BIT 概念中有 4 组 25 列。实际上字段是smallint,但它的数据是0或1。

这是我的代码,它试图获取第一组 25 列的总数。

Declare @rows int
, @ID uniqueidentifier
, @LocTotal bigint


select @rows =  ( select  count(*) from #t1 )

while @rows > 0
begin
print @rows
-- get that rowID
       select @ID = (select top 1 recid from #t1)
select @LocTotal =
(select top 1
case when cbHPILoc1 = 1 then 1 else 0 end +
case when cbHPILoc2 =  1 then 2 else 0 end +
case when cbHPILoc3 = 1 then  4 else 0 end +
< snip >
case when cbHPILoc25 = 1 then 16777216 else 0 end
as Total
 from  dbo.MyTest_BitMap
where RecKey = @ID
)
       print @ID
print  @LocTotal

我的输出:

(5 row(s) affected)
5
67A16306-B27D-4882-88A2-1146CBAAA8D9

(1 row(s) affected)
4
F94929B9-3DA7-4AA3-96F6-728EF025B21C

我没有得到总计@LocTotal

TIA

4

2 回答 2

3

为什么这么复杂?

SELECT
  RecKey,
  cbHPILoc1
  + cbHPILoc2 * 2
  + cbHPILoc3 * 4
  + ...
  + cbHPILoc25 * 16777216
  AS Total
FROM
  dbo.MyTest_BitMap
WHERE 
  RecKey = @ID
于 2009-05-01T16:47:49.083 回答
0

我认为 Tomalak 拥有它,这就是我对他进行改装的原因,但如果你多次或一次这样做,因为所有这些混乱:

cbHPILoc1
  + cbHPILoc2 * 2
  + cbHPILoc3 * 4
  + ...
  + cbHPILoc25 * 16777216

繁琐且容易出错,为什么不做一次作为视图并彻底测试呢?

那么就select total from viewname where reckey = ?.

于 2009-05-01T17:00:01.597 回答