9.4.10

Swapping column data in SQL Server

Swapping the column data between adjacent columns:

Scenario: I need to swap the column data in a table like column1 data to be moved to column2 and column2 data needs to be moved to column1
How can we achieve it?

Queries:

-- Creating the table and inserting records.
create table venkat (id int, id1 int)
insert into venkat values(1,2)
-- Check whether data is there
select * from venkat
-- Swapping the data
update venkat set id=id1,id1=id


In the above case, SQL Server will hold the data in memory until commit operation is commenced.
So, there is no need to hold the data in the third variable for swapping.

Cheers,
Venkatesan Prabu .J

1 comment: