我有一个表,在BIT概念中有4组25列。实际上field是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

我无法将Total转换为 @LocTotal

TIA

有帮助吗?

解决方案

我认为Tomalak拥有它,这就是为什么我对他进行了修改,但是如果你这样做了一次或多次,那就是所有这些混乱:

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

是单调且容易出错的,为什么不把它作为一个视图并彻底测试呢?

然后只需从viewname中选择总计,其中reckey =?

其他提示

为什么这么复杂?

SELECT
  RecKey,
  cbHPILoc1
  + cbHPILoc2 * 2
  + cbHPILoc3 * 4
  + ...
  + cbHPILoc25 * 16777216
  AS Total
FROM
  dbo.MyTest_BitMap
WHERE 
  RecKey = @ID
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top