Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

possible to do in 1 update 2

Status
Not open for further replies.

rds80

Programmer
Nov 2, 2006
124
US
Code:
DECLARE @TEMP TABLE
(
    idnum int IDENTITY(1, 1),
    col1 int,
    col2 int
)

INSERT INTO @TEMP(col1, col2) VALUES(2,3)
INSERT INTO @TEMP(col1, col2) VALUES(5,6)

SELECT * FROM @TEMP

Can do I a swap of the values in col 1 and col2 in 1 update query? Or do I need to create a temp table?

Thanks.
 
yes you can see example

Code:
DECLARE @TEMP TABLE
(
    idnum int IDENTITY(1, 1),
    col1 int,
    col2 int
)

INSERT INTO @TEMP(col1, col2) VALUES(2,3)
INSERT INTO @TEMP(col1, col2) VALUES(5,6)

SELECT * FROM @TEMP


update @TEMP set col1 =col2,col2 =col1


SELECT * FROM @TEMP

Denis The SQL Menace
SQL blog:
 
Yes, you can -

Code:
declare @temp table(
idnum int identity(1,1),
col1 int,
col2 int
)

insert into @temp values(2,3)
insert into @temp values(5,6)

select * from @temp

update @temp
set col1 = col2,
    col2 = col1

select * from @temp
 
wow, sorry guys I was thinking in c++ programming where I remember having to create a temp array to do the swapping..

this was simple
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top