Command to update multiple tables in the database

Asked

Viewed 116 times

1

It is possible to update my database as follows: My tables in the database have a field emp_codigo ? i would like to update the value of everyone that has in the database to 1

I don’t know if you understand, but example:

Have x tables with the same field emp_codigo and each table has a number x tuple and want to update all tuples of all tables in the field emp_codigo to receive value 1, it is possible?

2 answers

5


Jeff Henrique, good afternoon

There’s no way you can do this with a single command. You will have to assemble all Updates even, one per table, updating the value of your column emp_code.

3

There really isn’t a way to do everything in a single update. Depending on the frequency at which you need to do this, an output would be to create a precedent that receives the parameter to be persisted, and all the updates commands would be within this precedent.

Thus:

EXEC atualiza_dados meu_param

There inside your trial, would be the code with the updates.

CREATE PROCEDURE...
declare @meu_param
...

UPDATE table1 set emp_codigo = @meu_param where ...
UPDATE table2 set emp_codigo = @meu_param where ...
UPDATE table3 set emp_codigo = @meu_param where ...

That way, if you need to apply constant updates in the same tables again, just run the trial, since your idea is that all fields receive the same value.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.