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 RouseTask LeadAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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 RouseTask LeadAuthor 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
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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Crystal RouseTask LeadAuthor Commented:
OOPS!  I'll try to do a better job of explaining my issue.  I do appreciate your help though!!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott PletcherSenior DBACommented:
That's odd, since triggers don't have or accept parameters.
0
Crystal RouseTask LeadAuthor 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 RouseTask LeadAuthor Commented:
I ended up writing the trigger using parameters. Figured this one out on my own.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.