Hello World
Hello World

Reputation: 1437

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

Upvotes: 0

Views: 353

Answers (1)

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239646

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)

Upvotes: 2

Related Questions