Solved

SQL query help

Posted on 2013-06-22
3
308 Views
Last Modified: 2013-06-22
Hello there,

I have this four table

Province
County(FK is Provicne table id)
District(FK is County table id)
City (FK is District table id)

and I have this table called telcode which I got from excel which contains the province name and the city code and the tel code.

what I am trying to do now is compare my city table cityname with the cityname of the telcode and if it same then put the telcode from the telcode table into my city table. there are cases where I can have two city name in those tables.so I have added one more check i.e. to also check the province names. the telcode table contains more city which I might not have in my city table,so it needs to ignore those,so I have used LEFT OTHER JOIN

here is my query, but when I run the query I get error

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

UPDATE city SET telcode = (select telcode.telcode from city
INNER JOIN dbo.district ON city.districtID = district.id
INNER JOIN County ON county.id=district.countyID
INNER JOIN province ON province.id = county.provinceID
LEFT OTHER JOIN telcode ON city.cityname = telcode.city)

Open in new window

0
Comment
Question by:zolf
[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
  • 2
3 Comments
 
LVL 25

Accepted Solution

by:
jogos earned 500 total points
ID: 39267732
Syntax more like this (not tested )
UPDATE  c
 SET c.telcode = telcode.telcode 
from city c
INNER JOIN dbo.district ON c.districtID = district.id
INNER JOIN County ON county.id=district.countyID
INNER JOIN province ON province.id = county.provinceID
LEFT OTHER JOIN telcode ON c.cityname = telcode.city

Open in new window

0
 

Author Closing Comment

by:zolf
ID: 39267737
cheers!!
why did my query not work?? can you please explain
0
 
LVL 25

Expert Comment

by:jogos
ID: 39267808
This expects only one value
UPDATE SET   =  

Open in new window

This returns all values for all records
(select telcode.telcode from city
INNER JOIN dbo.district ON city.districtID = district.id
INNER JOIN County ON county.id=district.countyID
INNER JOIN province ON province.id = county.provinceID
LEFT OTHER JOIN telcode ON city.cityname = telcode.city)

Open in new window

And there is no way you indicate that there is a link .... all values returned must be updated in the matching record
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

635 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