I’d like to loop over a list of tables. For each table, I’d like to run an update query.
Psuedo code:
ArrayOfTablesObjects = {['tablename1','fieldname1'],['tablename2','fieldname2']......} foreach @tablename in ArrayOfTablesObjects UPDATE @tablename SET @fieldname = 'xyz' WHERE @fieldname = '123' end foreach
Thanks Harpo for your answer. I’ve used that idea to build the following sql statement. We have many tables 50+ that all have the same type of data (employee id) but that field name might be different. So, depending on the table name the field to be updated will be different. My actual WHERE and SET statement are more complicated than this example, but thats not important to this problem.
I first create a temporary table to store the table/fields I want to update.
I then loop over these records, and generate the SQL. For those of you who don’t like, dynamic sql, you can just use a print statement and then copy paste that into another query window and execute it. Or you can call the EXEC statement.
I’d like to accept your answers, but then didn’t quite answer my question, partly because I didn’t explain myself fully. Either way, thanks for your help.