Solved

query not working as expected

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Building JSON Results Table FROM DB 9 32
help converting varchar to date 14 25
Connect Gridview column to Textbox in C# 2 37
Not listening to where 1 17
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

762 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