Insert Trigger to Update a Field based on the value of an inserted record.

I need to update a field in a table on insert based on if the inserted value is null or not.

I have tried:

update [dbo].[TABLE1]
      set
    dbo.TABLE1.Assigned =
      
      CASE inserted.Field1
       When Null THEN LL.Lead
       ELSE 'Jane Doe'
      END      

    from
    inserted
    inner join
    dbo.TABLE2 LL
      on
    inserted.FieldName1 = LL.[FieldName]      
 where dbo.TABLE1.ID = inserted.ID
Crystal RouseWeb Developer/Database AdminAsked:
Who is Participating?
 
Crystal RouseConnect With a Mentor Web Developer/Database AdminAuthor Commented:
OOPS!  I'll try to do a better job of explaining my issue.  I do appreciate your help though!!
0
 
Scott PletcherSenior DBACommented:
update T1
      set
    T1.Assigned = COALESCE(inserted.Field1, 'Jane Doe')

    from
    inserted I
    inner join
    dbo.TABLE1 T1
      on
    T1ID = I.ID
    inner join
    dbo.TABLE2 LL
      on
    I.FieldName1 = LL.[FieldName]
0
 
Crystal RouseWeb Developer/Database AdminAuthor Commented:
Thanks, doesn't  COALESCE find the first Null value?  This works but I also need to update
the field if the value is not null with a value from Table 2.

So, if I use your example:

update Table1
      set
    Table1.Assigned = COALESCE(inserted.Field1, 'Jane Doe')

    from
    inserted
    inner join
    Table1
      on
    [Table1].[ID] = inserted.ID
    inner join
    dbo.Table2 LL
      on
    inserted.Field2 = LL.Field2
      AND
      inserted.Field3 = LL.Field3 AND inserted.Field4 = LL.Field4;

I need to update the Assigned Field with the value of Table2.Name if the inserted.Field1 is not null
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
Scott PletcherSenior DBACommented:
This works but I also need to update the field if the value is not null with a value from Table 2.

There's nothing in your original code that did that.

It would help if you could clearly state what you are trying to do.  Remember, we know nothing about your data or requirements except what you tell us.
0
 
Scott PletcherSenior DBACommented:
That's odd, since triggers don't have or accept parameters.
0
 
Crystal RouseWeb Developer/Database AdminAuthor Commented:
This Works:

BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;

      DECLARE @Username            varchar(255);
      DECLARE @Lead            varchar(255);
      DECLARE @FieldName      nvarchar(20);
      DECLARE @ID                        int;

      SET @Username = NULL;

      SET @Lead            = (SELECT ... AND inserted.Team = LL.Team)
      SET @FieldName      = (SELECT FieldName FROM inserted);
      SET @ID                        = (SELECT ID FROM inserted);
      
      IF @FieldName IS NULL
            BEGIN
                  SET @Username =  (SELECT ...));
            END
      ELSE
            BEGIN
                  SET @Username = (SELECT ...));
          END
0
 
Scott PletcherSenior DBACommented:
Ok.  Technically those are local variables not "parameters".  But it's all good, as long as you've got something that works for you.
1
 
Crystal RouseWeb Developer/Database AdminAuthor Commented:
I ended up writing the trigger using parameters. Figured this one out on my own.
0
All Courses

From novice to tech pro — start learning today.