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
 cursor and loop

Author  Topic 

Johnny Gunn
Starting Member

5 Posts

Posted - 2011-02-03 : 06:00:26
I have a table TABLE_A with columns ID and VALUE and TABLE_B with ID and VALUE. For each row in TABLE_B where TABLE_B.ID = TABLE_A.ID I want TABLE_B.VALUE to be updated from TABLE_A.VALUE.
I know this can be done with declaring a cursor and using loop but I don't know how to write it.

Anyone that can help?


regards Johnny Gunn

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-02-03 : 06:03:12
Update B
set b.value=a.value
from table_A as A inner join table_B as B
on A.ID = B.ID

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Johnny Gunn
Starting Member

5 Posts

Posted - 2011-02-03 : 07:07:35
Thanks, but I got error "Sql command not ended correctly" on the from keyword.

The specific update:
update tilsyn.etanlegg set a.hsikrfasetal = t.anleggnr
from tmp_jgg_anleggfase as t inner join tilsyn.etanlegg as a
on t.anleggnr = a.anleggnr

However, this is working:
select *
from tmp_jgg_anleggfase t inner join tilsyn.etanlegg a
on t.anleggnr = a.anleggnr

JG

regards Johnny Gunn
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2011-02-03 : 07:22:34
What is this? tilsyn.etanlegg

Just a table or Schema and a table or another DB's table
Go to Top of Page

Johnny Gunn
Starting Member

5 Posts

Posted - 2011-02-03 : 08:12:32
It's schema and table.

regards Johnny Gunn
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-02-03 : 08:24:58
update t
set a.hsikrfasetal = t.anleggnr
from tmp_jgg_anleggfase as t inner join tilsyn.etanlegg as a
on t.anleggnr = a.anleggnr

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

Johnny Gunn
Starting Member

5 Posts

Posted - 2011-02-03 : 11:32:51
Strange, but it doesn't work. I found some info on another forum where a person said that it works in Access but not in SQL. I think loop could solve it if anyone knows.

Anyway, thanks for feedback.

regards Johnny Gunn
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-02-04 : 04:39:56
quote:
Originally posted by Johnny Gunn

Strange, but it doesn't work. I found some info on another forum where a person said that it works in Access but not in SQL. I think loop could solve it if anyone knows.

Anyway, thanks for feedback.

regards Johnny Gunn


What did you mean by "It doesn't work"?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -