July 21, 2007 at 6:42 am
hi all,
While running a sql query, if i want to interchange the data of two rows. how can i do it?
e.g. suppose there are two table : T - A, T - B
I am running a sql query which includes both T - A and T - B.
There are two one column in both the tables with some data.
T - A column EMP ID
T - B column DEPT ID.
Now if i want to change the data of T - A to T - B and T - B to T - A.
How can i do it?
Thanks ,
Prajakta
July 21, 2007 at 9:06 am
Add a column to A
Copy B data to A's new Column
Copy A data to B's column
Copy new A column to old A column
Drop A's new column
July 21, 2007 at 9:47 am
I'm thinking there better be a reference in the two tables that directly relates the rows, as well...
--Jeff Moden
Change is inevitable... Change for the better is not.
July 21, 2007 at 10:07 am
Who needs that crap... wasn't it you that proposed a fully xmled Entity-value system? No need for all those table things anymore .
July 21, 2007 at 11:03 am
Now, THAT's funny!
--Jeff Moden
Change is inevitable... Change for the better is not.
July 21, 2007 at 12:55 pm
Smily,
They're making fun of you (in the good sense of that concept) because your problem sounds like you're from outer space. You need to come down to earth and describe your problem better to us simple folks.
July 21, 2007 at 1:04 pm
I'm not laughing at anyone... except at one comment Jeff made a few days (or weeks) ago.
July 21, 2007 at 6:24 pm
It was a funny comment, indeed.
No, we're not make fun of anyone except for each other
But, I was serious about needing to make a reference to the primary key... otherwise, there's no way to identify which employee row goes with which department row for the swap.
Me? I'd load a temp table with the primary key (whatever it is) and the employee id from the employee table along with a blank department id column. Then, I'd update that column, again, based on the primary key. Then, it's a simple matter of updating employee with department and vise versa.
Gotta ask... why on Earth would you want to do this? Perhaps a cleanup script to cleanup the data after maybe a bit of bad GUI code?
--Jeff Moden
Change is inevitable... Change for the better is not.
August 6, 2007 at 4:53 am
Still waiting here on Earth... why would you want to swap data between two obviously disparate tables?
--Jeff Moden
Change is inevitable... Change for the better is not.
August 14, 2007 at 3:12 am
hi all i was searching on google and found this thread...
i have an integer Sequence column in a table, i have a feature in the front end to swap the sequence between 2 rows. is there a single T-SQL command to do that?
August 14, 2007 at 4:58 am
UPDATE dbo.YourTable SET Seq = CASE Seq WHEN 1 THEN 2 WHEN 2 THEN 1 ELSE Seq END WHERE Seq IN (1, 2)
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply