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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 looping and updating
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

manish2aug
Starting Member

India
1 Posts

Posted - 05/04/2013 :  16:04:22  Show Profile  Reply with Quote
Hi,

Can somebody please help me to solve a problem, I am new to sql .

I have a table in which one column (Place) has some values like bellville, century city, constentia and ceres. Now in second table there are two columns property and value. Property column has values like population.rural.men.bellville.total, population.rural.men.constentia.total etc. Now I need to update the values of second table and iterate the first table to get the places. So in short like as follows

FOR [column(index) in table_1]

Begin

update table_2 set value = 'someValue' where key ='population.rural.men.'+column(index)+'.total'

END

What will be the sql for this?

Thanks

Manish

manish

James K
Flowing Fount of Yak Knowledge

3873 Posts

Posted - 05/05/2013 :  21:14:30  Show Profile  Reply with Quote
quote:
Originally posted by manish2aug

Hi,

Can somebody please help me to solve a problem, I am new to sql .

I have a table in which one column (Place) has some values like bellville, century city, constentia and ceres. Now in second table there are two columns property and value. Property column has values like population.rural.men.bellville.total, population.rural.men.constentia.total etc. Now I need to update the values of second table and iterate the first table to get the places. So in short like as follows

FOR [column(index) in table_1]

Begin

update table_2 set value = 'someValue' where key ='population.rural.men.'+column(index)+'.total'

END

What will be the sql for this?

Thanks

Manish

manish

Where do you get the 'someValue' that you want to set the value column to? If that data is in a table, then you can do a set-based to query to do the update.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52326 Posts

Posted - 05/06/2013 :  02:17:21  Show Profile  Reply with Quote
sounds like this to me

update t2 
set value = 'someValue' 
from table_2 t2
join table_1 t1
on property ='population.rural.men.'+ LTRIM(RTRIM(t1.Place)) +'.total'

As James suggested you could specify columnname instead of hardcoding if values reside in table column

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.17 seconds. Powered By: Snitz Forums 2000