Home

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

%3CLINGO-SUB%20id%3D%22lingo-sub-694469%22%20slang%3D%22en-US%22%3EUpdate%20a%20field%20of%20a%20SQL%20table%20using%20a%20trigger%20from%20another%20table-field%20(PK)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-694469%22%20slang%3D%22en-US%22%3E%3CDIV%20class%3D%22tw-swapa%22%3E%3CDIV%20class%3D%22DHcWmd%22%3E%26nbsp%3B%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%20class%3D%22oSioSc%22%3E%3CDIV%3E%3CDIV%20class%3D%22g9WsWb%22%3E%3CDIV%20class%3D%22tw-ta-container%20tw-nfl%22%3E%3CPRE%3E%3CSPAN%3EGood%20afternoon%20community%2C%0AI%20have%20the%20following%20tables%3A%3CBR%20%2F%3E%3CBR%20%2F%3Edbo.RegistroGeneral%3A%20%3CSTRONG%3EIdInpeccion%3C%2FSTRONG%3E%20(PK)%20int%2C%20%3CSTRONG%3EIdEquipo%3C%2FSTRONG%3E%20int%2C%20%3CSTRONG%3EIdEmpleado%3C%2FSTRONG%3E%20int%2C%20%3CSTRONG%3EFechaFin%3C%2FSTRONG%3E%20datetime%2C%20%3CSTRONG%3EFechaInicio%3C%2FSTRONG%3E%20datetime%3CBR%20%2F%3Edbo.RegistroPasos%3A%20%3CSTRONG%3EIdInspeccion%3C%2FSTRONG%3E%20(PK)%20int%2C%20%3CSTRONG%3EIdPasos%3C%2FSTRONG%3E%20nvarchar(5)%2C%20%3CSTRONG%3EObserPasos%3C%2FSTRONG%3E%20text%3CBR%20%2F%3E%3CBR%20%2F%3EWhat%20I%20need%20is%20that%20every%20time%20I%20enter%20a%20record%20in%20the%20table%20%3CSTRONG%3Edbo.RegistroGeneral%3C%2FSTRONG%3E%20a%20trigger%20take%20the%20value%20of%20the%3CBR%20%2F%3Eincremental%20key%20field%20%22%3CSTRONG%3EIdInspeccion%3C%2FSTRONG%3E%22%20and%20pass%20it%20or%20update%20it%20in%20the%20field%20%22%3CSTRONG%3EIdInspeccion%3C%2FSTRONG%3E%22%20of%20the%20table%20%3CSTRONG%3Edbo.RegistroPasos%3C%2FSTRONG%3E.%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20772px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F118213i095F3348182C8F77%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Captura.PNG%22%20title%3D%22Captura.PNG%22%20%2F%3E%3C%2FSPAN%3E%3CBR%20%2F%3EThe%20tables%20are%20related%20by%20the%20%3CSTRONG%3EIdInspection%3C%2FSTRONG%3E%20and%20both%20tables%20are%20filled%20from%20the%20execution%20of%20a%20Button.Run%20of%20PowerApp.%3CBR%20%2F%3EIn%20summary%20for%20the%20example%20in%20the%20image%20I%20need%20thefield%20%3CSTRONG%3EIdInspection%3C%2FSTRONG%3E%20to%20be%20equal%20to%201%20in%20the%2011%20registers%3CBR%20%2F%3Eof%20the%20table%20dbo.RegistroPasos%3C%2FSPAN%3E%3CSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3EI%20have%20never%20worked%20with%20triggers%20and%20as%20I%20have%20read%20it%20may%20be%20a%20solution%20to%20what%20I%20have%20been%20looking%20for.%3C%2FSPAN%3E%3C%2FPRE%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-695396%22%20slang%3D%22en-US%22%3ERe%3A%20Update%20a%20field%20of%20a%20SQL%20table%20using%20a%20trigger%20from%20another%20table-field%20(PK)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-695396%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F360938%22%20target%3D%22_blank%22%3E%40EMARIN91%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20could%20create%20a%20trigger%20on%20first%20table%20as%20follows%2C%20and%20it%20could%20work%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CBLOCKQUOTE%3E%3CP%3E%3CEM%3ECREATE%20TRIGGER%20TriggerName_Insert%20ON%20Table1%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3EFOR%20INSERT%20%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3EAS%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3EDECLARE%20%40newid%20as%20int%20%3B%3C%2FEM%3E%3C%2FP%3E%3CP%3E%3CEM%3E--%20gets%20the%20column%20information%20from%20the%20first%20table%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3ESET%20%40newid%20%3D%20(select%20i.ColumnName1%20from%20inserted%20i)%26nbsp%3B%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3EINSERT%20INTO%20Table2(ColumnName1)%20values%20(%40newid)%3C%2FEM%3E%3C%2FP%3E%3C%2FBLOCKQUOTE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFor%20more%20about%20triggers%2C%20you%20can%20have%20a%20look%20on%20below%20link.%20He%20explains%20all%20the%20trigger%20types%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fwww.codeproject.com%2FArticles%2F25600%2FTriggers-SQL-Server%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fwww.codeproject.com%2FArticles%2F25600%2FTriggers-SQL-Server%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHave%20a%20great%20day%20%3A)%3C%2Fimg%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
EMARIN91
Occasional Visitor
 
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.

Captura.PNG
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.
1 Reply

@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 :)

Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
35 Replies
Extentions Synchronization
Deleted in Discussions on
3 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
Security Community Webinars
Valon_Kolica in Security, Privacy & Compliance on
9 Replies