user3014081
user3014081

Reputation: 11

Inner query and joins for two tables

I have a ChildTable which i need to update its column.

Where condition value is picked from the Parent Table Id Column.

Update ChildTable set Column1 = 'Value', Column2 = 'Value2'
Where ChildTable.Id = 100

Upvotes: 0

Views: 45

Answers (2)

Vishwanath Dalvi
Vishwanath Dalvi

Reputation: 36591

First use select and make sure you're getting proper records to update.

as You've not given proper information but from your example I've come to this conclusion.

SELECT c_t.column1, c_t.column2
FROM   parent_table p_t inner join child_table c_t
ON     p_t.pk_column = c_t.fk_column
WHERE  c_t = 100;



UPDATE c_t
set    c_t.column1 = 'Value', c_t.column2 = 'Value2'
FROM   parent_table p_t inner join child_table c_t
ON     p_t.pk_column = c_t.fk_column
WHERE  c_t = 100;

Upvotes: 0

Tilak
Tilak

Reputation: 30698

Try following

Update ChildTable set Column1 = 'Value', Column2 = 'Value2' 
from ChildTable ct 
inner join parenttable pt on pt.key = ct.parentkey
Where ChildTable.Id = pt.parentconditionfield

Upvotes: 1

Related Questions