Reputation: 75
I have 2 tables:
t1
with columns A, B, X, Y, Z
andt2
with columns A, B, N
I want to set t1.A
to value t2.N
where t1.A = t2.A and t1.B = T2.B
.
It sounds simple, but I have no idea to solve this; I tried something like:
update t1 set A = (select t2.N
from t1,t2
where t1.A = t2.A
and t1.B = t2.B)
......
But this gives an error:
ERROR: You cannot reopen t1.DATA for update access with member-level control because t1.DATA .....
Any ideas?
Upvotes: 1
Views: 1356
Reputation: 2307
Make a copy of t1
(we will call it t1_copy
for example) and use the copy in your select statement. The reason for the table copy is you cannot select from a table you are updating.
%_eg_conditional_dropds(t1_copy); /* drop the copy before using it (if one exists) */
PROC SQL;
CREATE TABLE t1_copy AS
SELECT * FROM t1;
update t1 set A = (select t2.N
from t1_copy,t2
where t1_copy.A = t2.A
and t1_copy.B = t2.B)
QUIT;
Upvotes: 0
Reputation: 873
A side note besides the syntax error: you're getting that ERROR: You cannot open WORK.T1.DATA for output access with member-level control because WORK.T1.DATA is in use by you in resource environment DATASTEP.
because your output dataset is still open in the window. Close the VIEWTABLE:Work.T1
prior to re-running.
Upvotes: 0
Reputation: 1269503
I suspect you just want a correlated subquery:
update t1
set A = (select t2.N
from t2
where t1.A = t2.A and t1.B = t2.B
);
Note: You should be careful that the subquery returns only one row.
Upvotes: 1