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 Monday, September 30, 2013 12:43 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Today @ 6:08 AM
Points: 718, Visits: 541
You are right Dwain.

In such cases, we are mostly in a lose-lose situation. Either we will be rejected as we are not as idiot as we should be, or we will be selected to be ruled by an idiot.

If you are lucky then interviewer will reply at last "I was just cross verifying your knowledge."

Post #1499854
Posted Tuesday, October 1, 2013 2:31 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:57 AM
Points: 7,115, Visits: 13,476
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


Some background reading for you.


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1500301
Posted Tuesday, October 1, 2013 11:48 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 11:19 PM
Points: 36,724, Visits: 31,173
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.




That's when I'd smile, ask them if they have $5 to bet on it and a machine with Books Online on it to prove it. After I'd gotten my $5, I'd walk out of the interview. I've worked a places like that before and I don't want to do that again.


--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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1500521
Posted Tuesday, October 8, 2013 2:01 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, July 18, 2014 10:20 AM
Points: 110, Visits: 771
It seems we've moved into the "Dating Your Employer" thread
Post #1502815
Posted Tuesday, October 8, 2013 3:47 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 11:19 PM
Points: 36,724, Visits: 31,173
Tom Bakerman (10/8/2013)
It seems we've moved into the "Dating Your Employer" thread


Seems only natural... the original post was an interview question.


--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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1502849
Posted Tuesday, October 8, 2013 4:16 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 1:11 PM
Points: 7,019, Visits: 12,909
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?

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

Of course, this would be a rather pathetic discussion on thin ice, but at least it could bring you in the position of "it depends" (on the definition of the term "statement" in the given context) instead of "it's not possible" at all

And, as soon as "statement" is defined more precisely as "DML statement", the "back door" is closed and locked leaving a more detailed question:
Based on the example provided by dwain a few posts back, if we update a "left" table in a 1 ..n relationship, will sql server perform n updates or will it just do a single update? Will this example really change the value to 'B' and back to 'A'? How would you verify?

It's a rather weird original question. But with quite some room to open a discussion. Hidden. But available.




Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
Post #1502860
Posted Tuesday, October 8, 2013 4:36 PM


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 @ 5:18 PM
Points: 42,437, Visits: 35,492
LutzM (10/8/2013)
if we update a "left" table in a 1 ..n relationship, will sql server perform n updates or will it just do a single update?


Single.



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 #1502871
Posted Tuesday, October 8, 2013 4:59 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 1:11 PM
Points: 7,019, Visits: 12,909
GilaMonster (10/8/2013)
LutzM (10/8/2013)
if we update a "left" table in a 1 ..n relationship, will sql server perform n updates or will it just do a single update?


Single.

I know. But I thought it would make a good question.
Unfortunately, answered immediately. (as it is common here at SSC...)

But the list of questions can be continued:
If it's a single update, which value will be used? Will it be consistent/reproducable or may it vary under certain conditions (e.g. parallelism)?




Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
Post #1502875
Posted Wednesday, October 9, 2013 1:44 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 @ 5:18 PM
Points: 42,437, Visits: 35,492
LutzM (10/8/2013)
Will it be consistent/reproducable or may it vary under certain conditions (e.g. parallelism)?


The latter. Which is why those many-one updates have such a bad rap (deservedly). It's essentially another order without an order by scenario.



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 #1502947
Posted Wednesday, October 9, 2013 5:00 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, July 14, 2014 4:48 AM
Points: 2,834, Visits: 3,950
The question asked to OP by interviewer was probably like
"flip the values from A to B and B to A "
was actually answered by "Koen Verbeeck " in his first post here.

AS this is (i often experience this) very frequently asked question to people who are having exp from 3 to 5 yrs.


-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done
Post #1503017
« Prev Topic | Next Topic »

Add to briefcase «««1234»»

Permissions Expand / Collapse