Solved

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

Posted on 2016-08-09
12
35 Views
Last Modified: 2016-08-09
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
0
Comment
Question by:PeterBaileyUk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +2
12 Comments
 
LVL 50

Expert Comment

by:Vitor Montalvão
ID: 41748905
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);
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 41748906
The error's probably on line 3.  Qualify the name there (wt.clientcode or w.clientcode) and you should be fine.


Kent
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41748927
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

0
Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

 
LVL 65

Expert Comment

by:Jim Horn
ID: 41748935
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?
1
 

Author Comment

by:PeterBaileyUk
ID: 41748960
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
0
 

Author Comment

by:PeterBaileyUk
ID: 41748967
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

0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41748971
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

0
 

Author Comment

by:PeterBaileyUk
ID: 41748973
41748967 updated a similar table in access. obviously very different now in transact sql
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 41748976
Still not abundantly clear what you are trying to pull off here, but taking the above T-SQL and making the syntax correct.  

UPDATE wt
SET wt.ClientCode = w.ClientCode
FROM TblWords w
   LEFT JOIN TblWordTags wt ON w.ClientCodeWordPosition = wt.ClientCodeWordPosition 
WHERE wt.ClientCodeWordPosition Is Not Null

Open in new window


btw notice the elimination of unnecessary square brackets [] and parentheses marks ().
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 41748980
>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
1
 

Author Comment

by:PeterBaileyUk
ID: 41749008
saved everything then closed and opened sql server and the query on ID: 41748976 that i tried earlier now works.
0
 

Author Closing Comment

by:PeterBaileyUk
ID: 41749009
thank you
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Format Date fields 11 64
What is GIS method of Geometry data type? 6 33
SQL Server Error: 4060 8 32
SQL: Default Database Integrity Jobs Failing 6 16
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question