Swap data in two columns? 
Author Message
 Swap data in two columns?

I need to swap the data in two columns of a table.

I want to effect the swap with one update command.

thanks

richard



Sat, 26 Aug 2000 03:00:00 GMT
 Swap data in two columns?

Quote:

> I need to swap the data in two columns of a table.

> I want to effect the swap with one update command.

Easy enough:

1> create table fo (x int, y int)
2> go
1> insert fo values (1,9)
2> insert fo values (2,8)
3> go
(1 row affected)
(1 row affected)
1> update fo set x = y, y = x
2> go
(2 rows affected)
1> select * from fo
2> go
 x           y          
 ----------- -----------
           9           1
           8           2

(2 rows affected)

But how about with no update commands?

If the table is very large, it may well be
more efficient to use:

begin tran
sp_rename "mytable.col1", foobar
sp_rename "mytable.col2", col1
sp_rename "mytable.foobar", col2
commit tran

Note that any procedures, triggers, or views that access
the table should be dropped and recreated.

--
Bret Halford                    Imagine my disappointment
Sybase Technical Support        in learning the true nature            
3665 Discovery Drive            of rec.humor.oracle...
Boulder, CO 80303



Sat, 26 Aug 2000 03:00:00 GMT
 Swap data in two columns?

What about

 update OurTable set Field1=Field2, Field2=Field1

Quote:
> -----Original Message-----

> Posted At: Tuesday, March 10, 1998 12:11 PM
> Posted To: sybase
> Conversation:      Swap data in two columns?
> Subject:   Swap data in two columns?

> I need to swap the data in two columns of a table.

> I want to effect the swap with one update command.

> thanks

> richard



Sun, 27 Aug 2000 03:00:00 GMT
 
 [ 6 post ] 

 Relevant Pages 

1. swapping values in two rows?

2. Swapping between two Schema instances on a live system

3. Q: How to swap two tables?

4. Retrieve data from two tables, from two databases, in two registered servers

5. Error when joining two tables by two columns

6. join by two columns on two tables

7. Create one Column of Distinct Data from two tables

8. combine two columns from two tables?

9. Comparing two columns in two tables

10. Swapping Column values in SP

11. swap columns and rows

12. Splitting/swapping a column string


 
Powered by phpBB® Forum Software