relate two tables

Asked

Viewed 111 times

0

I would like to know how to relate two tables in different databases so that every time the value in the bank table1.dbo.table id1 will be changed also in the bank.dbo.table id1, modifying according to the line(id) which is being modified

the point is that the tables are like this

Tabela1
id - coluna2 - coluna3 - point
1 --- 0 ----------- 0 -------- 10
2 --- 0 ----------- 0 -------- 25

table 2
id - coluna2 - coluna3 - coluna4 - bonus
1 ------ 0 -------- 0 ---------- 0 --------- ?
2 ------ 0 -------- 0 ---------- 0 --------- ?

i wanted this point to pass pro bonus whenever it was changed, according to the row there (id), so in the case if moved that 25 there in row 2 will be changed also here in row 2 of the bonus column in table 2 is kind of confusing, thank you for responding

1 answer

1

This way you must get what you want:

-- Descomentar apenas se a solução abaixo não resolver o problema
-- EXEC sp_configure 'show advanced options', 1
-- GO
-- RECONFIGURE
-- GO
-- EXEC sp_configure 'nested triggers', 0
-- GO  
-- RECONFIGURE
-- GO

-- Alterar o "MyDataBase" para o nome da base de dados
ALTER DATABASE MyDataBase SET RECURSIVE_TRIGGERS OFF
GO

CREATE TRIGGER tr_AtualizaID ON tabela1
AFTER UPDATE   
AS
    DECLARE @Id     UNIQUEIDENTIFIER
    DECLARE @Points FLOAT

    IF UPDATE(points)
    BEGIN
        SELECT  TOP 1 
                @Id     = id
            ,   @Points = points
        FROM    inserted

        UPDATE  tabela2
        SET     bonus   = @Points
            ,   origem  = 1
        WHERE   id      = @Id
    END
GO

CREATE TRIGGER tr_AtualizaID ON tabela2
AFTER UPDATE   
AS
    DECLARE @Id     UNIQUEIDENTIFIER
    DECLARE @Points FLOAT

    IF UPDATE(points)
    BEGIN
        SELECT  TOP 1 
                @Id     = id
            ,   @Points = points
        FROM    inserted

        UPDATE  tabela1
        SET     bonus   = @Points
            ,   origem  = 1
        WHERE   id      = @Id
    END
GO

The initial comments serve to, when uncommented, avoid nesting triggers, but should only use if the solution to disable the recursion does not work.

  • @Merlin change your question and put that code there. The more detail on the question, the better!

  • i put below an example, but not leaving with the formwork that I wanted, I am new here on the forum, I do not know use yet right, thanks for your attention

  • Response changed to match the scenario you indicated. A recommendation: change the initial question instead of a new answer.

  • yes, I thank you too much, it worked perfectly for what I wanted, if it is not uncomfortable it takes me one more doubt, if I do this same procedure for another table, in the case for table 2, in case edit the 1 also edit the 2(already working), and if edit the 2 also edit the 1(I did but made a mistake when editing), I believe it is because one trigger keeps triggering the other endlessly? thanks for your attention

  • Then you’ll get one loop us triggers, so you’d have to do it another way. But you want to update which column of tabela1?

  • it would ask too much of me to ask you for a help in this matter, I do not have this knowledge, I have already researched by association of tables but nothing that would give exactly this result

  • Want to update the column points of tabela1 with the points of tabela2?

  • i wanted them to relate, like if one value is changed in the points column or the bonus column will be changed in the other too

  • Just create the Trigger likewise in tabela2, this if the column bonus is only updated from the column points, otherwise you will have a loop infinite.

  • this is the question, why it is basically like this, the points column increases with an algorithm based on time, from time to time it increases the value, and the bonus column is like a "money" so every time it is performed buying operation it decreases, so I wanted them to have a relationship, because then indirectly the purchase would be made with these "points"

  • Then I don’t understand... the column bonus of tabela1 is updated with the column value bonus of tabela2 and vice versa? Just like the column points? I was confused... before I said the column bonus was updated with the value of points!

  • yes the bonus column updates with the da points column, but the right thing would be that the opposite happens too, when changing the bonus also changes the point, I must have explained wrong

  • If so, then creating the same Trigger in the two tables will work! You just have to change the table in the Trigger of tabela2. Have you tried?

  • yes, I tried, but of an error speaking of the maximum level of nesting, I thought here if it would not be possible to put a conditional before this trigger, for it to test, for example if the two fields are different, will be activated the trigger, if they are equal not active, Why is it that when you change each other it changes, and then it’s going to be the same and the trigger stops being triggered, it doesn’t loop, it’s possible or I’m going too far? kk

  • Modified answer. Try the way I put it.

  • that "origin" there makes reference to what exactly? thanks for the attention always

  • I couldn’t understand how it for looping, continues with the same mistake =/

Show 12 more comments

Browser other questions tagged

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