简体   繁体   中英

Looping through a temporary table, comparing and setting values

I am looking for a way to be able to go through a CSV file or add the data of the CSV to a temporary table (I know how to do this part) and then compare the temporary table on one column with my permanent table and on the row it matches, set another column to a value within the temporary table.

if(Old_Url = Old Url)
{
  new_url = new_url
}

That's a bad code example of what I want to do as I have no idea how to show this in SQL

You don't loop (generally) in SQL - you write a query that applies to entire sets of rows.

It looks like you want some form of update:

UPDATE p
SET new_url = t.new_url
FROM PermanentTable p
INNER JOIN TemporaryTable t
ON p.old_url = t.old_url

(Although you should be cautious if TemporaryTable might contain multiple rows with the same old_url value and different new_url values - it's not well defined which values will be applied to any matching rows in PermanentTable )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM