0

I have the following table:

IDs     ZoneID     ChildID     ParentID
--------------------------------------------
null    1          36000       36000
null    1          36000       36000
null    2          37000       37000

I want to update the IDs column with the following select statement:

SELECT a.ZONEID FROM Table1 a INNER JOIN Table1 b on a.ParentID = b.ChildID

So, the IDs column will be filled with the result of the select statement above.

The result should be:

IDs     ZoneID     ChildID     ParentID
--------------------------------------------
1       1          36000       36000
1       1          36000       36000
2       2          37000       37000

I tried the following tsql:

Update Table1
set IDs = (SELECT a.ZONEID FROM Table1 a INNER JOIN Table1 b on a.ParentID = b.ChildID)

But error appears when I execute it. Too many argument result, something like that.

Please advice.
Thank you.

2 Answers 2

2

You can use join with a UPDATE query

Update Table1
set IDs = a.ZONEID 
FROM Table1 a INNER JOIN Table1 b on a.ParentID = b.ChildID
Sign up to request clarification or add additional context in comments.

Comments

1

You can use a correlated subquery:

Update Table1
    set IDs = (SELECT a.ZONEID FROM Table1 a WHERE a.ParentID = Table1.ChildID);

You don't need a JOIN in the subquery.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.