k_schultz k_schultz - 1 month ago 12
SQL Question

Trigger, Copy a column(which is used as a primary key) on insert to another table

I have a bunch of data that I export into a table. It already has a primary ID(HAZ_ID) I want to be able to copy that column to another table with the same column name(HAZ_ID) I tried creating a trigger

CREATE TRIGGER T_TableTokay_I
on dbo.test_tokay
after insert
as
set nocount on

insert into dbo.edit(inserted.HAZ_ID/* Columns in table b */)
select a.HAZ_ID /* Columns from table a */
from
test_tokay a


I try to insert it but it says dbo.edit cannot be NULL, so I'm assuming the data Im inserting into HAZ_ID in test_tokay isn't being sent to dbo.edit, how do i fix this?

Answer

If I understand you correctly, what you need to do is take the value from your inserted table into your edit table:

CREATE TRIGGER T_TableTokay_I
on dbo.test_tokay
after insert    
as
set nocount on

insert into dbo.edit(HAZ_ID)
select inserted.HAZ_ID