Solved

query not working as expected

Posted on 2013-06-19
5
193 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
cheers
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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.
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…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

763 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

15 Experts available now in Live!

Get 1:1 Help Now