I misremembered what the key was for this Templates table and therefore I added the wrong field as a foreign key. Now I need to add the foreign key and I want to populate its values based on this other field that is already populated. I started trying to do this with an update statement, but I'm not sure how to do it.
Part of my schema:
Products Table:
ProductName (key)
TemplateName
TemplateID
...
I've added TemplateID, but it has no data in it yet and is nullable.
Templates Table:
TemplateID (key)
TemplateName
...
I want to use the Templates table to find the matching TemplateID for each TemplateName in the Products table and fill that in the foreign key reference in the Products table.
Can I do this with a subquery in Update, or do I need to write some kind of Stored Proc? I'm using Sql Server 2008