Solved

How to create a trigger to loop through one table and insert those values plus a variable into another table

Posted on 2007-11-28
2
269 Views
Last Modified: 2010-04-21
Here's the scenario. I have an application and the result I want is that when a new employee is created, a trigger executes and adds all tasks from tbltask table into a tblTaskCompletion table for the new employee.

1. Jane Doe is added to tblEmployee
2. Insert trigger needs created, inserting multiple records into tblTaskCompletion - Within tblTask there are 50 new tasks that require completion for this employee - these must be added to tblTaskComletion (EmployeeID, TaskID) - EmployeeID was just created and upon insert, add all tasks from tblTask to tblTaskCompletion with new EmployeeID

I could do this within my application, but feel performance wise, this is the best route to go doing it via SQL Server. I'm using SQL Server 2005 by the way.

Thank you in advance for your assistance!!

0
Comment
Question by:cmcormick
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 500 total points
ID: 20365538
Loop is the wrong idea.  A trigger is the right idea, though.

create Trigger dbo.tblEmployee_ITrig
  On tblEmployee After Insert
AS
Insert Into tblTaskCompletion (EmployeeID, TaskID)
(Select I.EmployeeID, T.TaskID From Inserted I, tblTask T)
0
 

Author Closing Comment

by:cmcormick
ID: 31411415
Abolutely perfect! Thank you!
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Why do I get extra rows when I do inner join? 12 43
How can I get this column in my query? 2 62
SQL Server Insert where not exists 24 57
Using this function 4 53
Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question