We help IT Professionals succeed at work.

Inserting into table based on select plus additional value

cmcormick
cmcormick asked
on
284 Views
Last Modified: 2010-05-18
Below is the syntax I have in my stored procedure. I need to insert all records from tblTask with an actionID of 3 into tblTaskCompletion + the EmployeeID being passed to the stored procedure needs added. The procedure below only inserts one record. Any help is appreciated

select @TaskID = TaskID from tblTask where ActionID=3
if not exists (Select * from tblTaskCompletion where EmployeeID=@EmployeeID and TaskID=@TaskID)
begin
Insert Into tblTaskCompletion (EmployeeID, TaskID) VALUES (@EmployeeID, @TaskID)
end
Comment
Watch Question

Senior Database Administrator
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
select @TaskID = TaskID from tblTask where ActionID=3
the above only stores one record. ie single value.

try tis



if not exists (Select * from tblTaskCompletion where EmployeeID=@EmployeeID and TaskID IN (SELECT taskID from tblTask where ActionID=3)
begin
Insert Into tblTaskCompletion (EmployeeID, TaskID) SELECT @EmployeeID, TaskID from tblTask where ActionID=3)
end

Open in new window

Commented:
why dont you try using a cursor for selecting the task id

DECLARE CUR_TEST CURSOR FOR
SELECT TASKID FROM TBLTASK WHERE ACTIONID=3

OPEN CUR_TEST

FETCH NEXT FROM CUR_TEST INTO @TASKID

WHILE @@FETCH_STATUS ==0
BEGIN
      IF NOT EXISTS (SELECT * FROM TBLTASKCOMPLETION WHERE EMPLOYEEID=@EMPLOYEEID AND TASKID=@TASKID)
      BEGIN
      INSERT INTO TBLTASKCOMPLETION (EMPLOYEEID, TASKID) VALUES (@EMPLOYEEID, @TASKID)
      END
      
      FETCH NEXT FROM CUR_TEST INTO @TASKID

END

CLOSE CUR_TEST
DEALLOCATE CUR_TEST

Author

Commented:


The following is my stored procedure. I think this is the best solution once I get it working. dtodd, your solution added all the tasks with actionid=3 to all employees instead of just one. Wizilling, I couldn't get yours to work - I had tried a solution like that before. I think the cursor is the best solution.

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go





ALTER PROCEDURE [dbo].[spAddExitingTasks]
      -- Add the parameters for the stored procedure here
      @EmployeeID int
AS
BEGIN
declare @TaskID int
SET NOCOUNT ON;
DECLARE CUR_TEST CURSOR FOR
SELECT TASKID FROM TBLTASK WHERE ACTIONID=3

OPEN CUR_TEST

FETCH NEXT FROM CUR_TEST INTO @TASKID

WHILE @@FETCH_STATUS ==0
BEGIN
      IF NOT EXISTS (SELECT * FROM TBLTASKCOMPLETION WHERE EMPLOYEEID=@EMPLOYEEID AND TASKID=@TASKID)
      BEGIN
      INSERT INTO TBLTASKCOMPLETION (EMPLOYEEID, TASKID) VALUES (@EMPLOYEEID, @TASKID)
      END
     
      FETCH NEXT FROM CUR_TEST INTO @TASKID

END

CLOSE CUR_TEST
DEALLOCATE CUR_TEST

end

I get this error: Msg 102, Level 15, State 1, Procedure spAddExitingTasks, Line 28
Incorrect syntax near '='.


David ToddSenior Database Administrator
CERTIFIED EXPERT

Commented:
Hi,

WHILE @@FETCH_STATUS ==0

should be

WHILE @@FETCH_STATUS = 0

HTH
  David

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.