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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 looping and updating

Author  Topic 

manish2aug
Starting Member

1 Post

Posted - 2013-05-04 : 16:04:22
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
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-05-05 : 21:14:30
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

52326 Posts

Posted - 2013-05-06 : 02:17:21
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
   

- Advertisement -