Doubt with time recording data in master-detail tables?

Asked

Viewed 36 times

1

After entering a record, I use the select max(IDControle) to recover table id to record items.

When the time is only 1 second, I noticed that the items were all recorded in the first record. Does anyone have any idea how this could be avoided?

insert into TB_C_ORCAMENTO (descricaosituacao) values ('exemplo');
select max(controle) from TB_C_ORCAMENTO;
insert into TB_D_ORCAMENTO (controle, descricaoproduto) values (10, 'teste');
  • 1

    I didn’t quite understand your question, could you please explain a little better?

  • Put what you did, give example of what happens.

  • When you have 2 tables is the recording is made with a difference of 1 second I noticed that the items are recorded with the ID of the first query. Tell me what you don’t understand?

1 answer

0

This is not a very good way to get the last Id inserted. By competition conditions, this Id may have been changed between one command and another.

In this answer I put three alternatives that can be used. The context is within Stored Procedure, but can be used in your application code if the DbConnection is not closed.

Browser other questions tagged

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