Search code examples
sqlsql-servercoldfusioncfmllucee

Update a table based on another table


I am using a development environment with Lucee and SQL Server Management Studio. I have two tables:

  • Cities - Columns: city, state, region, and zip.
  • Info - Columns: city, state, zip

There are 80,000+ entries Info. I ONLY want to update the zip column in the Cities table. It is all null for each of the 22,000 entries. If there is a matching value for City and State, assign the value to Cities based off Info. I haven't figured out how to go through the whole query like that without having some kind of search value.

I've tried things like the code below. It outputs 10 from Cities, and the last index [9] from Info in 2 dumps. Why does it not display each iteration of i? It could be 30,000 in order to go through the whole query but still how do I update based on a value in Info and do it ONLY ONCE even if there are multiple matching values in Info. Also, if matching values do not exist in Info, how to not set any incorrect information?

<cfquery name="list" datasource="source">
    SELECT TOP 10 *
    FROM cities
    ORDER BY CITY ASC
</cfquery>

<cfparam name="i" default="1">
<cfloop query="#list#" from="1" to="10" index="i">
  <cfoutput>#list.city[i]#</cfoutput>
  <cfoutput>#list.state[i]#</cfoutput>
</cfloop>
<cfquery name="master" datasource="source">
    SELECT TOP 10 *
    FROM WEBMASTERCOPY
    WHERE STATE = '#list.state[i]#' AND city = '#list.city[i]#'
</cfquery>

Solution

  • It ended up being like this:

     update Cities
       set Cities.ZIP = Info.ZIP from Info 
       where Cities.STATE = Info.STATE
     and 
    Cities.CITY = Info.CITY