0

Ive hit a wall with what seems a simple procedure.

Update a table from another table, heres where i am:

DECLARE @oldcode  varchar(50)
DECLARE @newcode  varchar(50)


SET @oldcode = table1.OLDCODE
SET @newcode = table1.NEWCODE


UPDATE table2 SET [CODE] = @NEWCODE WHERE [CODE] = @OLDCODE

this results in: The multi-part identifier "table1.OLDCODE" could not be bound.

cheers :-

the data:

Table1

record  OLDCODE NEWCODE
1   ZZZALF38    ALF38
2   ZZZALF38.1  ALF38.1
3   ZZZALF38.2  ALF38.2

table2

record  CODE    
1   ZZZALF38    
2   ZZZALF38.1  
3   ZZZALF38.2  

wish to change table 2 to:

record  CODE    
1   ALF38   
2   ALF38.1 
3   ALF38.2 
3
  • 2
    Please tag your question with the database you are using. You should also show sample data for table1 and table2. Commented Nov 7, 2014 at 15:40
  • sorry.... a data example is:Table1 record OLDCODE NEWCODE 1 ZZZALF38 ALF38 2 ZZZALF38.1 ALF38.1 3 ZZZALF38.2 ALF38.2 table2 record CODE 1 ZZZALF38 2 ZZZALF38.1 3 ZZZALF38.2 wish to change table 2 to: record CODE 1 ALF38 2 ALF38.1 3 ALF38.2 Commented Nov 7, 2014 at 15:48
  • @user3641134, sample data would be far better off as an edit to your original post - please click the word 'edit' under the question, and put the values there. Commented Nov 7, 2014 at 15:51

2 Answers 2

2

I am guessing you are using SQL Server and this is what you want:

update t2
    set code = t1.newcode
    from table2 t2 join
         table1 t1
         on t2.code = t1.oldcode;
Sign up to request clarification or add additional context in comments.

1 Comment

will your query work in Oracle ?
0
update table2 set code=table1.newcode from table1  on table2.code=table1.code

1 Comment

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.

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.