Click here to monitor SSC
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
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

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

any single statement to complete this task


Um...

GilaMonster (9/25/2013)
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, MVP, M.Sc (Comp Sci)
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


GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47285 Visits: 44392
Koen Verbeeck (9/25/2013)
Sure.


SELECT 'Nothing' FROM myTable;



Et voila, the row still has the value A.


I can simplify that...

;




Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
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


Kenny Jozi
Kenny Jozi
Say Hey Kid
Say Hey Kid (684 reputation)Say Hey Kid (684 reputation)Say Hey Kid (684 reputation)Say Hey Kid (684 reputation)Say Hey Kid (684 reputation)Say Hey Kid (684 reputation)Say Hey Kid (684 reputation)Say Hey Kid (684 reputation)

Group: General Forum Members
Points: 684 Visits: 1275
DECLARE @t TABLE(city VARCHAR(1))
INSERT INTO @t(city)
VALUES ('A'),( 'B'),( 'C'),( 'A'),( 'B'),( 'C'),( 'A');

-- do a select and not the results for comparison purposes

SELECT city FROM @t


UPDATE @t
SET city = CASE city
WHEN 'A' THEN 'B'
WHEN 'B' THEN 'A'
else city
END

-- select after update to compare with initial select
select * from @t

SQL 2000/2005/2008/2012 DBA - MCTS/MCITP
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4259 Visits: 6431
I'll get into the spirit of this thing you've got going here!


CREATE TABLE #MyTable
(
city nchar(10)
);

INSERT INTO #MyTable VALUES ('A');

WITH SampleData (city) AS
(
SELECT 'A' UNION ALL SELECT 'B' UNION ALL SELECT 'A'
)
UPDATE a
SET city = b.city
FROM #MyTable a
JOIN SampleData b ON 1=1;

SELECT * FROM #MyTable;

GO
DROP TABLE #MyTable;




:-P


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
dastagiri16
dastagiri16
SSC-Enthusiastic
SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)

Group: General Forum Members
Points: 118 Visits: 491
hi,

here i am not able to trust it is updated with 'A'

supose in cte last select statement can be changed to 'AA' it is not showing last updated record that is 'AA'


Thanks,
Dastagiri
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

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

here i am not able to trust it is updated with 'A'

supose in cte last select statement can be changed to 'AA' it is not showing last updated record that is 'AA'


Thanks,
Dastagiri



In fact, it is only updated once:
http://www.sqlservercentral.com/articles/T-SQL/101464/


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
T.Ashish
T.Ashish
Right there with Babe
Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)

Group: General Forum Members
Points: 767 Visits: 582
@dastagiri16,

I'm sure you was not applying for the job of DBA.
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45147 Visits: 39923
dastagiri16 (9/25/2013)
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....



Not all questions asked by interviewers have a positive answer. Sometimes they ask the "wrong" thing to see what you'll say.

Also, not all interviewers actually have the knowledge to conduct a proper interview. They might actually but incorrectly believe that you can update the same row more than once in the same "query".

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
T.Ashish
T.Ashish
Right there with Babe
Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)Right there with Babe (767 reputation)

Group: General Forum Members
Points: 767 Visits: 582
I have faced the same situation in an interview where interviewer asked me a question on triggers.

I tried my hard to convince him that we have INSTEAD OF and AFTER triggers in MS sql-server, and he was stuck to his point that we also have a BEFORE trigger in MS sql-server.
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4259 Visits: 6431
T.Ashish (9/29/2013)
I have faced the same situation in an interview where interviewer asked me a question on triggers.

I tried my hard to convince him that we have INSTEAD OF and AFTER triggers in MS sql-server, and he was stuck to his point that we also have a BEFORE trigger in MS sql-server.




In cases like that, after you've made your initial point that you don't think they exist, it is probably best to stick with something non-committal like:

"I'd be fascinated to learn about BEFORE triggers from you some time, but for now I can't comment further because I have no experience with them."

One of the guys on this forum has a saying in his signature link that goes something like:

"Never argue with an idiot. They'll drag you down to their level and they've got a lot more experience operating there."


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
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