Link to home
Start Free TrialLog in
Avatar of mandelia
mandeliaFlag for India

asked on

Update field in one table by looking it in second table.

Well there just one problem. the second table may have multiple entries. so consider table A

table A:

ROLLNUMBER   NAME
111                     na
111                     na

TABLE B:

ROLLNUMBER  ALIAS NAME
111                    GM     GAURAV
111                    MAN   GAURAV
111                    GAU   GAURAV


NOW I NEED TO UPDATE NAME IN TABLE A LOOKING IT UP IN TABLE B. INNER JOIN WILL GIVE ME 6 ENTRIES BUT I NEED JUST THE NAME SO AFTER UPDATE I SHOULD HAVE JUST 2 ROWS IN TABLE A.

THANKS



Avatar of mandelia
mandelia
Flag of India image

ASKER

Dont worry the names are not not different and even if they are something like  First(name)  will solve my purpose
Avatar of Guy Hengel [angelIII / a3]
syntax depends on the database product/version you use,
please clarify

note: please do not post with ALL CAPS ON...
that's usually considered shouting...
right now MS Access.
 
oh ok that was not intentional
 
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks for the frist part how do in do this in MS SQL  server 2005.
moreover is there a query that can do the same on multiple servers
i mean database independent
in sql, this would be:
UPDATE a
  SET Name = b.Name
FROM TABLEA a
 JOIN TableB b
  on B.RollNumber = a.RollNumber

Open in new window

in oracle:
UPDATE a
  SET Name = (SELECT b.Name FROM TableB b WHERE B.RollNumber = a.RollNumber and rownum = 1 )
FROM TABLEA a

Open in new window

in mysql:
UPDATE TableA a, TableB b
  SET a.Name = b.Name
WHERE B.RollNumber = a.RollNumber

Open in new window

>i mean database independent
no way, sorry.

the best is you put that code in a stored procedure, so "all" you have to do to make the application "portable" is to change/adjust the stored procedures ...
but B.RollNumber = a.RollNumber will return 3 rows
wont it create a problem
 
tried  ....
 
UPDATE [TABLEA]
SET NAMEA = DLOOKUP ("NAMEB","TABLEB". "rollB =" & rollA)
 
as column names in table a are rollA and NameA
and in tableB are nameB and rollB
 
 
It gives me invalid use of '.','!' or '()'. in query expression 'DLOOKUP ("NAMEB","TABLEB". "rollB =" & rollA'.
i have more than 1M rows so using update and Dlookup in VBA is out of question.
i think it will take a lot of time
sorry my fault there was a . insteed of , after tableB.
 
It worked
Brilliant
The query ran fine but didnot update giving a type conversion error.  All fields here is text any idea what can it be
> All fields here is text any idea what can it be
UPDATE [TABLEA]
SET NAMEA = DLOOKUP ("NAMEB","TABLEB". "rollB = """ & rollA & """ ") 
  

Open in new window