May 29, 2012

sp_MSsetbit (Transact-SQL MetaData) Definition

Please note: that the following source code is provided and copyrighted by Microsoft and is for educational purpose only.
The meta data is from an SQL 2012 Server.

I have posted alot more, find the whole list here.

Goto Definition or MetaData

Definition:

sys.sp_MSsetbit(smallint @coltoadd
, int @toset)

MetaData:

 create procedure sys.sp_MSsetbit  
@bm varbinary(128) output,
@coltoadd smallint,
@toset int = 1
AS
declare @bytenum smallint
declare @bit smallint
declare @mask tinyint
declare @newbyte tinyint
declare @oldbyte tinyint

SELECT @bytenum = 1 + FLOOR((@coltoadd-1)/8)

IF @bytenum > 128 return 0

SELECT @bit = (@coltoadd-1) % 8

SET @mask = POWER(2, @bit)
if @toset = 0
SET @mask = (~@mask % 256)

if @bm is null
set @bm = 0x0
while datalength(@bm) < @bytenum
set @bm = @bm + 0x00

SET @oldbyte = SUBSTRING( @bm, @bytenum, 1)
IF @oldbyte IS NULL SET @oldbyte = 0
if @toset <> 0
SET @newbyte = @oldbyte | @mask
else
SET @newbyte = @oldbyte & @mask

if (@bytenum = 1)
set @bm = convert(binary(1), @newbyte) + substring(@bm, 2, 127)
else
set @bm = substring(@bm, 1, @bytenum - 1) + convert(binary(1), @newbyte) + substring(@bm, @bytenum + 1, 128 - @bytenum)

No comments:

Post a Comment

Total Pageviews