February 10, 2011 at 3:01 am
I need to UPDATE a column in multiple rows with a different value for each WHERE condition.
My updates are being done as individual queries like this:
SQL Code:
update s
set s.clean_city = l.output_city ,
s.input_city= l.input_city from mst_Address as s
inner join digitlookups.dbo.city_lookup as l
on s.clean_country=l.city_country and
replace(addressline5,' ','')= L.Input_City
WHERE clean_city is null
and automatic_process_desc is null
update s
set s.clean_city = l.output_city ,
s.input_city= l.input_city from mst_Address as s
inner join digitlookups.dbo.city_lookup as l
on s.clean_country=l.city_country
and ' '+ addressline5 + ' ' LIKE '% ' + L.input_City + ' %'
where clean_city is null and DBO.GEOEXCLUSION_CHECKER(AddressLine5) = 1
and automatic_process_desc is null
update s
set s.clean_city = l.output_city ,
s.input_city= l.input_city from mst_Address as s
inner join digitlookups.dbo.city_lookup as l
on s.clean_country=l.city_country
and ' '+ addressline5 + ' ' LIKE '% ' + L.input_City + ' %'
where clean_city is null and DBO.GEOEXCLUSION_CHECKER(AddressLine5) = 0
and city_include_exclude = '0'
and automatic_process_desc is null
Is there any way to do something like this in a single query? or is is there any efficient way of doing this?
Thanks.
February 10, 2011 at 3:23 pm
You could combine the second two like this
update s
set s.clean_city = l.output_city ,
s.input_city= l.input_city from mst_Address as s
inner join digitlookups.dbo.city_lookup as l
on s.clean_country=l.city_country
and addressline5 LIKE '%' + L.input_City + '%'
where clean_city is null
and automatic_process_desc is null
and
(
DBO.GEOEXCLUSION_CHECKER(AddressLine5) = 1
OR
(
DBO.GEOEXCLUSION_CHECKER(AddressLine5) = 0
and city_include_exclude = '0'
)
)
I took out the extra space before and after addressline5 since it was not needed.
I don't know that there is much way to combine this with the first one because the join conditions are different.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
February 11, 2011 at 12:37 pm
Sean Lange (2/10/2011)
I don't know that there is much way to combine this with the first one because the join conditions are different.
The OR operator is valid in JOIN conditions - just join on (a=b and c=d) OR (a=c and b=d).
Jason Wolfkill
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply