SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


how to update column city value from 'A' to 'B' and 'B' to 'A' in single query


how to update column city value from 'A' to 'B' and 'B' to 'A' in single query

Author
Message
dan-572483
dan-572483
Hall of Fame
Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)

Group: General Forum Members
Points: 3487 Visits: 2000
Depending on the interviewer's definition of "query" it could be
BEGIN TRAN
UPDATE ADDRESS SET CITY = 'B'
ROLLBACK TRAN


Tom Bakerman
Tom Bakerman
Right there with Babe
Right there with Babe (735 reputation)Right there with Babe (735 reputation)Right there with Babe (735 reputation)Right there with Babe (735 reputation)Right there with Babe (735 reputation)Right there with Babe (735 reputation)Right there with Babe (735 reputation)Right there with Babe (735 reputation)

Group: General Forum Members
Points: 735 Visits: 931
LutzM (10/8/2013)
If you insist doing it "in single statement", how about this:

EXEC (...).

If we'd agree this would qualify as a single statement, we're all set. Right? :-D

To "support my argument": BOL qualifies EXEC as a statement: ...SQL Server extends the EXECUTE statement...



In this vein, is a BEGIN .. END block considered a single statement?
nitinkachhwaha
nitinkachhwaha
SSC Journeyman
SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)

Group: General Forum Members
Points: 92 Visits: 34
Hi,

Please find the query to update the cty to 'A' where cty is 'B' and from cty 'B' to 'A'.
BEGIN TRAN

SELECT *FROM CITY

UPDATE CITY
SET CTY = CASE WHEN CTY = 'A' THEN 'B'
WHEN CTY ='B' THEN 'A'
END

SELECT *FROM CITY

ROLLBACK
T.Ashish
T.Ashish
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1341 Visits: 607
nitinkachhwaha (10/10/2013)
Hi,

Please find the query to update the cty to 'A' where cty is 'B' and from cty 'B' to 'A'.
BEGIN TRAN

SELECT *FROM CITY

UPDATE CITY
SET CTY = CASE WHEN CTY = 'A' THEN 'B'
WHEN CTY ='B' THEN 'A'
END

SELECT *FROM CITY

ROLLBACK



Well done. Great reply.Hehe
Fal
Fal
Right there with Babe
Right there with Babe (770 reputation)Right there with Babe (770 reputation)Right there with Babe (770 reputation)Right there with Babe (770 reputation)Right there with Babe (770 reputation)Right there with Babe (770 reputation)Right there with Babe (770 reputation)Right there with Babe (770 reputation)

Group: General Forum Members
Points: 770 Visits: 1809
dastagiri16 (9/25/2013)
hi,

I have a table like

id city
1 A

so i want to update city column from A to B and again B to A by in single statement..please help.


Thanks
Dastagiri



So, update it twice, use single statement. No problem!



update myTable
set City =
case
when City = A then B
when City = B then A
end

go 2





According to BOL "GO is not a Transact-SQL statement; it is a command" ;-)


Fal.
Koen Verbeeck
Koen Verbeeck
SSC Guru
SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)

Group: General Forum Members
Points: 60943 Visits: 13297
Fal (10/10/2013)
dastagiri16 (9/25/2013)
hi,

I have a table like

id city
1 A

so i want to update city column from A to B and again B to A by in single statement..please help.


Thanks
Dastagiri



So, update it twice, use single statement. No problem!



update myTable
set City =
case
when City = A then B
when City = B then A
end

go 2




According to BOL "GO is not a Transact-SQL statement; it is a command" ;-)



Genius! :-D


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
david.john.gilligan
david.john.gilligan
Mr or Mrs. 500
Mr or Mrs. 500 (517 reputation)Mr or Mrs. 500 (517 reputation)Mr or Mrs. 500 (517 reputation)Mr or Mrs. 500 (517 reputation)Mr or Mrs. 500 (517 reputation)Mr or Mrs. 500 (517 reputation)Mr or Mrs. 500 (517 reputation)Mr or Mrs. 500 (517 reputation)

Group: General Forum Members
Points: 517 Visits: 550
from BOL: "GO is not a Transact-SQL statement; it is a command recognized by the sqlcmd and osql utilities and SQL Server Management Studio Code editor."

That is nice!!!!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search