title: need help building a stored procedure that takes rows from one table into another
body: <p>alright i built this stored procedure to take the columns from a stagging table and copy them into my other table, but if these four columns are duplicates it wont insert the rows, works fine.</p>
  
  <p>however, what i want to do is if only the tour, taskname and deptdate are the same, then i will update the rest of the information. and if all four columns are  the same dont instert.</p>
  
  <pre><code>INSERT INTO dashboardtasks1
      SELECT [tour], [taskname], [deptdate], [tasktype], [desc], [duedate], [compdate], [comments], [agent], [compby], [graceperiod]
      FROM staggingtasks
      WHERE NOT EXISTS(SELECT * 
                       FROM dashboardtasks1 
                       WHERE (staggingtasks.tour=dashboardtasks1.tour and
                           staggingtasks.taskname=dashboardtasks1.taskname and 
      staggingtasks.deptdate=dashboardtasks1.deptdate and 
     staggingtasks.duedate=dashboardtasks1.duedate 
      )
                       )
  </code></pre>
  
  <p>i saw something like this </p>
  
  <pre><code>INSERT INTO table (a,b,c) VALUES (1,2,3)
    ON DUPLICATE KEY UPDATE c=c+1;
  
  UPDATE table SET c=c+1 WHERE a=1;
  </code></pre>
  
  <p>but how could i do it if my stated 3 columns are the samed then update?</p>
  
  <p>or is there a way to do this with an if statement and use 2 different queries, but how would my if statement work would it check if the row exists in the table i am uploading to and then run the insert statement?</p>
  
  <p>or what if i did something like</p>
  
  <pre><code>alter PROCEDURE test
  
  AS
  BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;
  
      -- Insert statements for procedure here
  
  
  update dashboardtasks set
      duedate = s.duedate
  from staggingtasks as s
      where s.tour=dashboardtasks.tour and
              s.taskname=dashboardtasks.taskname and 
          s.deptdate=dashboardtasks.deptdate
  
  
  INSERT INTO dashboardtasks
  SELECT [tour], [taskname], [deptdate], [tasktype], [desc], [duedate], [compdate], [comments], [agent], [compby], [graceperiod]
  FROM staggingtasks
  WHERE NOT EXISTS(SELECT * 
                   FROM dashboardtasks 
                   WHERE (staggingtasks.tour=dashboardtasks.tour and
                         staggingtasks.taskname=dashboardtasks.taskname and 
  staggingtasks.deptdate=dashboardtasks.deptdate and 
  staggingtasks.duedate=dashboardtasks.duedate 
  )
                   )
  
  
  
  
  END
  GO
  </code></pre>
  
  <p>staging</p>
  
  <pre><code> 62 3647    Request Space   3/30/2011   Land    NULL    1/6/2010    NULL    NULL    PEGGYH      NULL    NULL
  
      81  505 Rel. Space  02/22/2012  Land    NULL    12/24/2011  NULL    NULL    IMANA       NULL    NULL
  
      82  505 Ticket  02/22/2012  Air NULL    1/8/2012    NULL    NULL    SYLVIAT     NULL    NULL
  
      83  505 Names to Airlines   02/22/2012  Air NULL    1/8/2012    NULL    NULL    SYLVIAT     NULL    NULL
  
      90  505 Names to Airlines   02/22/2012  Air NULL    1/1/2012    NULL    NULL    SYLVIAT     NULL    NULL
  
     92   505 Names to Airlines   02/22/2012  Air NULL    1/1/2012    NULL    NULL    SYLVIAT     NULL    NULL
  </code></pre>
  
  <p>table</p>
  
  <pre><code>1   3647    Request Space   3/30/2011   Land    NULL    11/6/2010   NULL    NULL    PEGGYH      NULL    NULL
  
      2   505 Rel. Space  02/22/2012  Land    NULL    11/24/2011  NULL    NULL    IMANA       NULL    NULL
  
      3   505 Ticket  02/22/2012  Air NULL    11/8/2012   NULL    NULL    SYLVIAT     NULL    NULL
  
      4   505 Names to Airlines   02/22/2012  Air NULL    11/8/2012   NULL    NULL    SYLVIAT     NULL    NULL
  </code></pre>
  
  <p>results</p>
  
  <pre><code>  1 3647    Request Space   3/30/2011   Land    NULL    1/6/2010    NULL    NULL    PEGGYH      NULL    NULL
  
      2   505 Rel. Space  02/22/2012  Land    NULL    12/24/2011  NULL    NULL    IMANA       NULL    NULL
  
      3   505 Ticket  02/22/2012  Air NULL    1/8/2012    NULL    NULL    SYLVIAT     NULL    NULL
  
      4   505 Names to Airlines   02/22/2012  Air NULL    1/8/2012    NULL    NULL    SYLVIAT     NULL    NULL
  
     5    505 Names to Airlines   02/22/2012  Air NULL    1/1/2012    NULL    NULL    SYLVIAT     NULL    NULL
  
     6    505 Names to Airlines   02/22/2012  Air NULL    1/1/2012    NULL    NULL    SYLVIAT     NULL    NULL
  </code></pre>