需要帮助构建一个存储过程,从一个表到另一个表

时间:2011-01-31 14:37:15

标签: sql sql-server-2005 stored-procedures

好吧我构建了这个存储过程来从一个不稳定的表中取出列并将它们复制到我的另一个表中,但如果这四列是重复的,它就不会插入行,工作正常。

然而,我想要做的是如果只有tour,taskname和deptdate是相同的,那么我将更新其余的信息。并且如果所有四列都相同,则不要进行操作。

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 
    )
                     )

我看到了类似的东西

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;

但是如果我说明的3列相同然后更新,我怎么能这样做呢?

或者有没有办法用if语句执行此操作并使用2个不同的查询,但是我的if语句如何工作以检查我上传到的表中是否存在该行,然后运行insert语句? / p>

或者如果我做了类似的事情

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

分期

 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

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

结果

  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

2 个答案:

答案 0 :(得分:2)

这可能适合你。

update dashboardtasks1 set
  tasktype = s.tasktype,
  [desc] = s.[desc],
  duedate = s.duedate,
  compdate = s.compdate,
  comments = s.comments,
  agent = s.agent,
  compby = s.compby,
  graceperiod    = s.graceperiod
from staggingtasks as s
where
  s.tour=dashboardtasks1.tour and
  s.taskname=dashboardtasks1.taskname and 
  s.deptdate=dashboardtasks1.deptdate and
  not exists (select *
              from dashboardtasks1 as d
              where s.tour=d.tour and
                    s.taskname=d.taskname and 
                    s.deptdate=d.deptdate and
                    s.duedate=d.duedate
             )        


insert into dashboardtasks1 (tour, taskname, deptdate, tasktype, [desc], duedate, compdate, comments, agent, compby, graceperiod)
select tour, taskname, deptdate, tasktype, [desc], duedate, compdate, comments, agent, compby, graceperiod
from staggingtasks as s
where not exists (select *
                  from dashboardtasks1 as d
                  where s.tour=d.tour and
                        s.taskname=d.taskname and 
                        s.deptdate=d.deptdate and
                        s.duedate=d.duedate
                 )

答案 1 :(得分:0)

LEFT OUTER JOIN可以解决问题

INSERT INTO dashboardtasks1 (tour, taskname, deptdate, tasktype, desc, duedate, compdate, comments, agent, compby, graceperiod)
SELECT s.tour, s.taskname, s.deptdate, s.tasktype, s.desc, s.duedate, s.compdate, s.comments, s.agent, s.compby, s.graceperiod
FROM staggingtasks s
LEFT OUTER JOIN  dashboardtasks1 d ON d.tour=s.tour and d.taskname=s.taskname and d.deptdate=s.deptdate
WHERE d.tour IS NULL

你在这里做的是加入你想要匹配的值的两个表。

如果dashboardtasks1中没有一行存在您加入的值(在本例中为tour,taskname和deptdate),则会插入一行。否则就不会。