DBA > Job Interview Questions > Microsoft SQL Server FAQs

What is the result of the following query?

More DBA job interview questions and answers at http://dba.fyicenter.com/Interview-Questions/

(Continued from previous question...)

What is the result of the following query?

What is the result of the following query?
declare @a int
declare @b int

set @a = 5
set @b = 11

select @a = @a ^ @b , @b = @b ^ @a, @a = @a ^ @b
print '@a = '+convert(varchar,@a)
print '@b = '+convert(varchar,@b)


Answer
@a = 11 , @b = 5

Explanation
Here is the explanation:

Step 1
@a has the value of 5 and is binairy 101
@b has the value of 11 and is binairy 1011

After “select @a = @a ^ @b , @b = @b ^ @a, @a = @a ^ @b”
the values are
@a will get the value from 101 ^ 1011 = 1110 (=14)
@b have stil the value of 1011 (=11)

Step 2
After “select @a = @a ^ @b , @b = @b ^ @a, @a = @a ^ @b”
The values are
@a will stay at the value 1110 (=14)
@b will get the value from 1011 ^ 1110 = 101 (=5)

Step 3
After “select @a = @a ^ @b , @b = @b ^ @a, @a = @a ^ @b”

@a will get the value from 1110 ^ 101 = 1011 (=11)
@b will stay at the value 101 (=5)

(Continued on next question...)

Other Job Interview Questions