Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 1234»»»

how to update column city value from 'A' to 'B' and 'B' to 'A' in single query Expand / Collapse
Author
Message
Posted Wednesday, September 25, 2013 1:21 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Yesterday @ 11:57 PM
Points: 72, Visits: 253
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
Post #1498153
Posted Wednesday, September 25, 2013 1:25 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:43 AM
Points: 13,565, Visits: 11,375
UPDATE myTable
SET City = CASE WHEN City = A THEN B
WHEN City = B THEN A
END





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

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1498154
Posted Wednesday, September 25, 2013 1:36 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Yesterday @ 11:57 PM
Points: 72, Visits: 253
hi,

The below query only updating the city value from A to B not B to A

Please advice.

Thanks,
Dastagiri
Post #1498161
Posted Wednesday, September 25, 2013 1:38 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 2:03 AM
Points: 40,445, Visits: 36,898
Koen's query will work because SQL does updates in two phases, first where it reads the data second where it changes.
If it's not doing what you want, please post actual data and desired results (and the table's definition) so we can see what's happening.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1498164
Posted Wednesday, September 25, 2013 1:44 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Yesterday @ 11:57 PM
Points: 72, Visits: 253
hi,

one of the interviewer asked the question about updating...so i have added test table like below...

USE [Practice]
GO

/****** Object: Table [dbo].[mytable] Script Date: 09/25/2013 13:10:59 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[mytable](
[city] [nchar](10) NULL
) ON [PRIMARY]

GO

and i have inserted values like


insert into mytable (city) values ('A')


so now i want to write update querty to update city value from A to B then B to A by using any query....

Post #1498165
Posted Wednesday, September 25, 2013 1:48 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:43 AM
Points: 13,565, Visits: 11,375
Ah ok, so you want to update a value to a new value and then back to it's old value.
It seems rather pointless, are you sure that's what the interviewer meant?




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

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1498169
Posted Wednesday, September 25, 2013 1:53 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Yesterday @ 11:57 PM
Points: 72, Visits: 253
Koen,

Yes , i agree with you but i don't know its possible..i was not replied any thing when he raised that question becuase i thought that by using any recursive cte it will work.. So i searching for that..
Post #1498173
Posted Wednesday, September 25, 2013 1:53 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 2:03 AM
Points: 40,445, Visits: 36,898
So update it, then update it back?

Update MyTable
SET City = 'B'
WHERE City = 'A';

Update MyTable
SET City = 'A'
WHERE City = 'B';

You can't do it in one statement, because updating a column to one value and then updating it to another require two statements. Though you could just have a single statement that does nothing and it'll have the same effect.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1498174
Posted Wednesday, September 25, 2013 1:59 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Yesterday @ 11:57 PM
Points: 72, Visits: 253
hi,

any single statement to complete this task
Post #1498176
Posted Wednesday, September 25, 2013 2:05 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:43 AM
Points: 13,565, Visits: 11,375
Sure.

SELECT 'Nothing' FROM myTable;

Et voila, the row still has the value A.




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

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1498179
« Prev Topic | Next Topic »

Add to briefcase 1234»»»

Permissions Expand / Collapse