Author |
Topic |
SamC
White Water Yakist
3467 Posts |
Posted - 2004-09-29 : 11:56:04
|
Given a couple of variables: A and B, exchange the values without using temporary variable or storage space. |
|
Arnold Fribble
Yak-finder General
1961 Posts |
Posted - 2004-09-29 : 12:13:30
|
Very oldskool!DECLARE @a AS int, @b AS intSET @a = 44SET @b = 83SELECT @a, @bSET @a = @a ^ @bSET @b = @a ^ @bSET @a = @a ^ @bSELECT @a, @b |
 |
|
SamC
White Water Yakist
3467 Posts |
Posted - 2004-09-29 : 12:21:54
|
Yeah, more of a poll I suppose. Hard to get the counts among readers tho.... |
 |
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-09-29 : 13:52:31
|
This also works I can't ever remember to use the bitwise operations...Declare @a int, @b intSet @a = 2Set @b = 7Select @a, @bSelect @a = B.a, @b = B.b From (Select a=@a, b=@b) A Inner Join (Select a=@b, b=@a) Bon A.a = B.b and A.b = B.aSelect @a, @b Corey |
 |
|
ditch
Master Smack Fu Yak Hacker
1466 Posts |
Posted - 2004-09-29 : 15:02:43
|
Nice one Corey!Duane. |
 |
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2004-09-29 : 15:18:44
|
wow -- very cool Arnold ! definitely old-school !this works for a and b both less than 10000:DECLARE @a AS int, @b AS intSET @a = 44SET @b = 83SELECT @a, @bset @b = @b + @a * 10000set @a = @b - @a * 10000set @b = @b / 10000select @A,@B- Jeff |
 |
|
ditch
Master Smack Fu Yak Hacker
1466 Posts |
Posted - 2004-09-29 : 15:26:41
|
Intereseting solution Jeff.Corey's solution can also work with non-numeric data though.[CODE]Declare @a varchar(10), @b varchar(10)Set @a = 'You'Set @b = 'Me'Select @a, @bSelect @a = B.a, @b = B.b From (Select a=@a, b=@b) A Inner Join (Select a=@b, b=@a) Bon A.a = B.b and A.b = B.aSelect @a, @b[/CODE]Duane. |
 |
|
SamC
White Water Yakist
3467 Posts |
Posted - 2004-09-29 : 15:27:20
|
Good idea Corey,Wouldn'tSELECT @A = A, @B = B FROM (SELECT @A as B, @B as A) X do the same? |
 |
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2004-09-29 : 15:32:28
|
i don't count that as not using extra store -- you are allocating an entire virtual table or two. - Jeff |
 |
|
Arnold Fribble
Yak-finder General
1961 Posts |
Posted - 2004-09-29 : 15:36:24
|
quote: Originally posted by SamCWouldn'tSELECT @A = A, @B = B FROM (SELECT @A as B, @B as A) X do the same?
Irrespective of whether it should, it doesn't work when I try it -- the query optimizer squishes it down to a constant scan and consequently serializes it as two assignments with no (implied) temporary. |
 |
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-09-29 : 15:36:33
|
quote: Originally posted by SamC Good idea Corey,Wouldn'tSELECT @A = A, @B = B FROM (SELECT @A as B, @B as A) X do the same?
I thought that as well, but it seems that it interprets that as:Set @a = @bSet @b = @aI am not 'technically' defining an extra store Corey |
 |
|
X002548
Not Just a Number
15586 Posts |
Posted - 2004-09-29 : 15:41:59
|
No, but you're getting it.....Brett8-) |
 |
|
ditch
Master Smack Fu Yak Hacker
1466 Posts |
Posted - 2004-09-29 : 15:44:54
|
These people now days!All they want to do is Get It Duane. |
 |
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-09-29 : 15:46:19
|
sue me... Corey |
 |
|
Pat Phelan
Posting Yak Master
187 Posts |
Posted - 2004-10-01 : 18:57:50
|
The best that I can actually get to work is:DECLARE @a CHAR(1), @b CHAR(1)SELECT @a = '1', @b = '2'SELECT @a, @bSELECT @a = b.b, @b = a.a FROM (SELECT @a AS a) AS a CROSS JOIN (SELECT @b AS b) AS bSELECT @a, @b You ought to be able to use:DECLARE @a CHAR(1), @b CHAR(1)SELECT @a = '1', @b = '2'SELECT @a, @bSELECT @a = @b, @b = @aSELECT @a, @b ...but it doesn't work according to the SQL-92 specs for deferred assignment (but then we already knew that about MS-SQL)!-PatP |
 |
|
|