Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 update table query

Author  Topic 

pnpsql
Posting Yak Master

246 Posts

Posted - 2011-12-29 : 08:14:59
hi team , i have a table with a city column, i need to update the city like where city name is 'A' then it should be 'B' and when city name is
'B' then it should be 'A'


challenge everything

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-12-29 : 08:20:23
[code]
update atable
set city = case when city_name = 'A' then 'B'
else 'A'
end
where city_name in ('A', 'B')
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

pnpsql
Posting Yak Master

246 Posts

Posted - 2011-12-29 : 08:21:23

i do it my way, please help with cte or any other way....




SELECT * FROM abc1
update abc1
set city = ( case when (CITY= 'A') then 'B'
else 'B' END )




challenge everything
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-12-29 : 08:33:38
oh ! i see you mean same column and not city and city_name.

what do you mean by "please help with cte or any other way" ?

What's the issue with that query ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

pnpsql
Posting Yak Master

246 Posts

Posted - 2011-12-29 : 08:35:03
this is gentle question, can it be done by any other way.??


challenge everything
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-12-29 : 08:51:49
this is the easiest way.

Not sure why are you looking for in CTE, but cte will not make it any easier


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-12-29 : 09:07:52
quote:
Originally posted by pnpsql

this is gentle question, can it be done by any other way.??


challenge everything



I'm guessing you mean a simple question

WHY would you want to drop a Nuclear Bomb on a problem, when ou ave a simple solution?

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-12-29 : 09:23:51
From your original post, it seemed like you wanted to swap the city names - i.e., if it is 'A', then you want to change it to 'B' and if it is 'B' you want to change it to 'A'. If that is so, you can do it
UPDATE abc1 SET
city =
CASE
WHEN city = 'A' THEN 'B'
WHEN city = 'B' THEN 'A'
END
WHERE
city IN ('A','B');
Go to Top of Page

pnpsql
Posting Yak Master

246 Posts

Posted - 2011-12-29 : 09:26:02
ha ha ha...thanks for driving me in right direction..

challenge everything
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-12-29 : 09:45:58
You are quite welcome.

As an aside, when I first started programming in SQL, I found it very difficult to see how this type of query would work correctly. Coming from a C# world, I thought that:

1) it would change all the 'A's to 'B's, and THEN
2) it would change all the 'B's (including those that were changed in the prior step) to 'A's

thus ending up with all 'A's.

When I realized that that is not the case, that SQL treats it as a SET with a "BEFORE" and "AFTER" concept, I was floored! I stood up and did a hula dance in my office (and luckily no one was around to see)
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-12-29 : 09:49:02
First one wins...very simple...oh wait...you're not a COBOL Mainframer

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

pnpsql
Posting Yak Master

246 Posts

Posted - 2011-12-29 : 22:57:01
what does it means...


challenge everything
Go to Top of Page
   

- Advertisement -