SQL: Exchange column values -


Want to clear some concepts about SQL Internal.

Suppose I have a table:

  --------- tblXY ----------- x int y int < / Code> 

Now there is a record in it:

  XY --- 1 4 2 3 3 2 4 1  

And I The result table should be:

  XY --- 4 1 3 2 2 3 1 4  

So I wrote this question:

  UPDATE tblXY SET [X] = Y, [Y] = X  

and found the necessary results.

But how did this happen? I mean that I am setting the value of x as the current value of y and at the same time I am setting the value of y as the x.

That's because operations are an atomic action - X and The current value of> Y is done before any of the functions.

It is not too high:

  for each line: set x = y set y = x  

but more Like:

Set
  for each line: tmpx = x set tmpy = y set x = tmpy set y = tmpx  

Keep in mind that The only conceptual approach is that it is likely to be more efficient under cover.

Without it, you must temporarily store yourself for each line, or just rename the column: -)


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -