At times you may need to swap the values of two columns of the same SQL Server table. For example, suppose that you realized that the firstname column of the Employees table contains the lastast name of each employee, and vice versa.
We can take advantage of T-SQL and perform this operation much faster, This is possible by using T-SQL temporary variables and the capability to assign and use them inside an UPDATE statement. This is the T-SQL statement that performs the swap:
Here i have used Northwind database's Employees table
DECLARE @tmp VARCHAR(50) UPDATE employees SET @tmp=firstname, firstname=lastname, lastname=@tmp
|
No responses found. Be the first to respond and make money from revenue sharing program.
|