Solved

query not working as expected

Posted on 2013-06-19
5
195 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 48

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 31

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 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

914 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now