Sunday, June 1, 2014

t-sql 2008 r2 explain select


create function dbo.solveBitVal(@val bigint)
returns @result table(id int, bitvalue bigint)
begin
declare @bitTranslate table (id int, bitvalue bigint)
declare @numbah table (num int)
declare @loopah int
set @loopah = 1
while @loopah <= 63
begin
insert into @bitTranslate (id, bitvalue) values (@loopah, POWER(convert(bigint,2),@loopah-1))
insert into @numbah (num) values (@loopah)
set @loopah = @loopah + 1
end
while @val > 0
begin
insert into @result
select max(id), max(bitvalue) from @bitTranslate where bitvalue <= @val
select @val = @val - max(bitvalue) from @bitTranslate where bitvalue <= @val
end
return
end

select * from dbo.solveBitVal(1337)



This might be a good start for a function that translates bitvalues into their ID's



This function should do the trick.

No comments:

Post a Comment