I am trying to write a SQL update query which will loop through several tables and update values.
I have a cus_no in several tables and I want to create a temporary table in SQL with the old cus_no and the new cus_no value and have loop through a list of tables and change the values in each of those tables.
Is cus_no part of any foreign key relationships in these tables? If so, it gets a little more complicated. Either way, there is no need to loop. You can join to the temp table and update all the values at once. This would be the set based approach which is more efficient than looping.
If it is part of the foreign key, you won't be able to update the values. You will have to insert records with the new values and then delete the old records. You also need to make sure you insert the parent records first and delete the child records first.
Greg
0
Question has a verified solution.
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.
Greg