Forum Discussion

EMARIN91's avatar
EMARIN91
Copper Contributor
Jun 15, 2019

Update a field of a SQL table using a trigger from another table-field (PK)

 
Good afternoon community,
I have the following tables:

dbo.RegistroGeneral: IdInpeccion (PK) int, IdEquipo int, IdEmpleado int, FechaFin datetime, FechaInicio datetime
dbo.RegistroPasos: IdInspeccion (PK) int, IdPasos nvarchar(5), ObserPasos text

What I need is that every time I enter a record in the table dbo.RegistroGeneral a trigger take the value of the
incremental key field "IdInspeccion" and pass it or update it in the field "IdInspeccion" of the table dbo.RegistroPasos.


The tables are related by the IdInspection and both tables are filled from the execution of a Button.Run of PowerApp.
In summary for the example in the image I need thefield IdInspection to be equal to 1 in the 11 registers
of the table dbo.RegistroPasos


I have never worked with triggers and as I have read it may be a solution to what I have been looking for.
  • shah_chandra's avatar
    shah_chandra
    Copper Contributor

    EMARIN91 

     

    You could create a trigger on first table as follows, and it could work

     

    CREATE TRIGGER TriggerName_Insert ON Table1
    FOR INSERT
    AS
    DECLARE @newid as int ;

    -- gets the column information from the first table
    SET @newid = (select i.ColumnName1 from inserted i) 
    INSERT INTO Table2(ColumnName1) values (@newid)

     

     

    For more about triggers, you can have a look on below link. He explains all the trigger types 

    https://www.codeproject.com/Articles/25600/Triggers-SQL-Server

     

     

    Have a great day :)

Resources