Update table after an Insert

I am looking for a trigger that will update my table after an Insert.

Table to run trigger on "TEMP"
When a record is entered I want to take the Field "MTSS_NAME" and go to the table called "ANALYST"

Analyst Table Contains the Fields "EMAIL" and "ManagerName"

Find all of the Email with the "ManagerName = to Mtss_Name" and create a string separated with a ";" and update the table "TEMP" with that string into the field "DistrictDL"

Can someone help Please.
Who is Participating?
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.

Patrick MatthewsCommented:

With respect, that is not a good idea.  For example, while this can be done to get the value correct at the INSERT time, what happens if later on the emails on the analyst table change?

You are better off to instead use a select query at the time you need the data.  This is rather easily done using FOR XML PATH, assuming you are on SQL 2005 or later.

xeroxcanadaAuthor Commented:
Sorry but I am really green here.
I need to update the record as it is entered into the database.
The emails are inserted once.
If it really must be done, I would use a BEFORE INSERT trigger in DB2 to set the value of the column before it gets written to the table. I'm not sure of a good reason for doing it as an update after it's written.

I don't have appropriate experience with SQL Server to provide a reliable example.

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!

xeroxcanadaAuthor Commented:
Final trigger that works.


ON [dbo].[temp]

DECLARE @p1 varchar (50)  
DECLARE @p2 varchar (10)

      @p1 = MTSS_Name,
      @p2 = Escal_ID,

FROM Inserted

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

    -- Insert statements for procedure here

INSERT temp_email (Entry, Email)

SELECT Entry, Email
FROM Analyst  
WHERE ManagerName = @p1

--(select * from temp_email)

declare @sqlstr varchar(max),
            @crtid integer, @Getmail varchar (max)

set  @sqlstr =  ''

DECLARE email_list CURSOR FOR (SELECT Entry  FROM temp_email)
OPEN email_list
      FETCH next FROM email_list INTO @crtid
      WHILE @@fetch_status=0
            set @sqlstr = @sqlstr + (select email from temp_email where Entry = @crtid) + ';'
      FETCH next FROM email_list INTO @crtid
CLOSE email_list
DEALLOCATE email_list
DELETE temp_email
--Print @sqlstr
EXEC CreateDL @sqlstr,@p2


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
Anthony PerkinsCommented:
I realize you are have stated you are "really green here", however, you should know that TRIGGERs fire once per statement and not once per row.  Clearly the way you have it coded does not apply when more than one row is INSERTed.
xeroxcanadaAuthor Commented:
I Need to understand this a bit more.
I have a form that does an Insert into the DB and inserts a new row.
So when that happens the trigger fires.

So if the form is sent twice lets say at the same time will the trigger fire twice or once?
The chance of someone filling in the form at the exact same time is very minimal.
I only get about 30 inserts in an 8-12 hour period.
So what am I missing?
Anthony PerkinsCommented:
So what am I missing?
If your INSERT statement inserts more than one row, the TRIGGER will fire only once and in consequence @p1 and @p2 will contain the values from one row, even though INSERTED contains more than one row. If this will never happen (a single user INSERTs more than one row at one time) than you should be able to sleep fine.
xeroxcanadaAuthor Commented:
zzzzzzzzzzzzzzzzzzzzzzzzzzz...Thank You for clearing that up.
xeroxcanadaAuthor Commented:
Figured it out
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
Microsoft SQL Server

From novice to tech pro — start learning today.