Link to home
Start Free TrialLog in
Avatar of PeterBaileyUk
PeterBaileyUk

asked on

created a new field in sqlserver table but cannot update it.

Ive added a new column to my tblwordtags table and I can see it in the column of the table i would like to update it, it is underlining the column the error message is;
Msg 209, Level 16, State 1, Line 5
Ambiguous column name 'ClientCode'.

this works
select *
from TblWordTags wt
LEFT JOIN tblwords w ON w.clientcodeWordPosition = wt.clientcodeWordPosition;

Open in new window


this does not
Use Dictionary
update TblWordTags
set wt.clientcode=(      'error here
select ClientCode
from TblWordTags wt
LEFT JOIN tblwords w ON W.clientcodeWordPosition = wt.clientcodeWordPosition);

Open in new window


its strange if you look at the screenshot the column is not showing the field to choose.
2016-08-09.png
Avatar of Vitor Montalvão
Vitor Montalvão
Flag of Switzerland image

Try to add the table name to the column name:

update TblWordTags
set clientcode=(      'error here
select wt.ClientCode
from TblWordTags wt
LEFT JOIN tblwords w ON W.clientcodeWordPosition = wt.clientcodeWordPosition);
The error's probably on line 3.  Qualify the name there (wt.clientcode or w.clientcode) and you should be fine.


Kent
an update statement should not contain a select clause:
update wt
set clientcode= whichfielddoyouwanthere
from TblWordTags wt
LEFT JOIN tblwords w ON W.clientcodeWordPosition = wt.clientcodeWordPosition

Open in new window

Couple things wrong with your query:

  • Line 2 : Uses the alias wt., but wt only lives in the subquery in lines 3-5, so line 2 does not know what wt. is.  Remove it.
  • Line 3-5:  Are you certain that that query will return one and only one row?  If not, you'll get a 'Subquery cannot return more than one row' error message, as line 2 attempts to assign a single value to the result of a subquery, which cannot return more than one row.
  • Looks like you're setting tblWordTags.ClientCode to TblWordTags.ClientCode.  Are you sure that is correct?
Avatar of PeterBaileyUk
PeterBaileyUk

ASKER

tblwords contains the field clientcode with clientcodewordposition as pk in both tables and i want to get each clientcodewordposition that exists in the tblwordtags and update its clientcode to the same value.

the select works here are the rows attached
ee.JPG
I thought this might work but it doesnt

UPDATE TblWords LEFT JOIN TblWordTags ON TblWords.ClientCodeWordPosition = TblWordTags.ClientCodeWordPosition SET TblWordTags.ClientCode = [TblWords].[ClientCode]
WHERE (((TblWordTags.ClientCodeWordPosition) Is Not Null));

Open in new window

that would be:
update wt
set clientcode= w.clientcode
from TblWordTags wt
LEFT JOIN tblwords w ON W.clientcodeWordPosition = wt.clientcodeWordPosition

Open in new window

41748967 updated a similar table in access. obviously very different now in transact sql
ASKER CERTIFIED SOLUTION
Avatar of Jim Horn
Jim Horn
Flag of United States of America 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
>updated a similar table in access. obviously very different now in transact sql
For a handy guide in converting Access SQL to T-SQL check out  Migrating your Access Queries to SQL Server Transact-SQL
saved everything then closed and opened sql server and the query on ID: 41748976 that i tried earlier now works.
thank you