Friday, February 24, 2012

Can somebody explain what this is doing... select ...status & 4096 ...

Can somebody explain what "status & 4096" is doing in this SQL:
"select name, (status & 4096) as 'SingleUser' from sysdatabases.
In SQL Books, I find reference to "bitwise" operation. In my case, a database has a status value of 4104 (which means "single user" and "trunc. log on chkpt"). This statement returns a value of 4096 for this particular database. I want to understand wh
y a 4096 (and 0 for ones that are not "single user") is being returned.
Thanks
> I want to understand why a 4096 (and 0 for ones that are not "single
user") is being returned.
From the SQL 2000 Books Online:
<Excerpt href="http://links.10026.com/?link=tsqlref.chm::/ts_operator_7fax.htm">
The bitwise & operator performs a bitwise logical AND between the two
expressions, taking each corresponding bit for both expressions. The bits in
the result are set to 1 if and only if both bits (for the current bit being
resolved) in the input expressions have a value of 1; otherwise, the bit in
the result is set to 0.
<Excerpt>
Since the binary value of 4096 is 0001000000000000, a single bit is
evaluated and the expression 'status & 4096 will yeild either 0 or 4096.
This allows the single-user status bit to be examined independently of the
other bit values.
Hope this helps.
Dan Guzman
SQL Server MVP
"john lantz" <anonymous@.discussions.microsoft.com> wrote in message
news:060E090D-18F1-4649-980B-FF4FB64DB205@.microsoft.com...
> Can somebody explain what "status & 4096" is doing in this SQL:
> "select name, (status & 4096) as 'SingleUser' from sysdatabases.
> In SQL Books, I find reference to "bitwise" operation. In my case, a
database has a status value of 4104 (which means "single user" and "trunc.
log on chkpt"). This statement returns a value of 4096 for this particular
database. I want to understand why a 4096 (and 0 for ones that are not
"single user") is being returned.
> Thanks

No comments:

Post a Comment