Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

query not working as expected

Posted on 2013-06-19
5
Medium Priority
?
205 Views
Last Modified: 2013-06-21
Hello there,

I have this query

UPDATE district
SET district.countyID = county.ID
FROM county as c INNER JOIN district ON c.county = district.county
INNER JOIN county on county.province = district.province;

Open in new window


which should compare 2 cols in county table and district table and if they are same then get the id of the county table and insert in the district countyid col. but it is not working. it is only checking for the province name and inserting the id.
0
Comment
Question by:zolf
5 Comments
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39258936
not sure about this, could you try this as a select first

--UPDATE district
--SET district.countyID = c.ID

select c.id, d.*

FROM district as d
INNER JOIN county AS c  ON district.county = c.county
AND district.province = c.province
0
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 39259179
UPDATE district
SET district.countyID = county.ID
FROM county as c INNER JOIN district ON c.county = district.county
INNER JOIN county on county.province = district.province;




which should compare 2 cols in county table and district table and if they are same then get the id of the county table and insert in the district countyid col. but it is not working. it is only checking for the province name and inserting the id.


hi,

as per my understanding,
you are joining two tables with country and province.

so common row only will update, why you joining twice?
UPDATE district
SET district.countyID = county.ID
FROM county as c INNER JOIN district ON c.county = district.county
AND c.province = district.province;

Open in new window


if you want to update based on country then
UPDATE district
SET district.countyID = county.ID
FROM county as c INNER JOIN district ON c.county = district.county

Open in new window

0
 
LVL 32

Expert Comment

by:awking00
ID: 39259797
Can you provide some sample data for the two tables and your expected results after the update?
0
 
LVL 5

Accepted Solution

by:
DOSLover earned 2000 total points
ID: 39260350
Hi Zolf,
Based on what you are saying, it looks like ID is the Primary/Unique key on County table. The District table has two columns (among many) -  County and Province - using which you can uniquely get a County record. The District table also has column named CountyId which for some reason is not correct (or may be a new column you have added). If that is true, the following statement should help to set the correct CountyId.

UPDATE district d
       SET countyID = 
       (Select c.ID
         FROM county c 
        where c.county = d.county
            and c.province = d.province)

Open in new window

0
 

Author Closing Comment

by:zolf
ID: 39267531
cheers
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

916 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