【问题标题】:need help building a stored procedure that takes rows from one table into another需要帮助构建一个将行从一个表转移到另一个表的存储过程
【发布时间】:2011-01-31 14:37:15
【问题描述】:

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

但是,我想要做的是,如果只有游览、任务名称和部门日期相同,那么我将更新其余信息。如果所有四列都相同,请不要插入。

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 语句将如何工作它会检查我正在上传到的表中是否存在该行然后运行插入语句?

如果我做了类似的事情会怎样

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

【问题讨论】:

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


    【解决方案1】:

    这可能对你有用。

    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
                     )
    

    【讨论】:

    • 谢谢你,我现在就试试,这可能是个愚蠢的问题,但我是在 if 语句中这样做的,或者在另一个之后这样做
    • 您是否运行了插入语句?您必须同时运行更新和插入。顺便说一句,插入与您的相同。我没有改变它。
    • 尝试使用答案中的第二个更新语句。我相信这是你应该使用的。
    • 我试过了,我把我用过的东西贴在了上面,但这很奇怪,因为我添加了一个完全相同的行并且它没有更新任何一个嗯,我一定是错过了什么
    • duedate 是唯一正在更新的列,所以这是我更新中唯一的列吗?
    【解决方案2】:

    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),它将插入一行。否则不会。

    【讨论】:

    • 我的查询确实确保该行不重复,我的问题是游览任务名称和部门日期是否相同,但到期日期不同我想更新该行而不是插入另一行相同的数据但不同的截止日期。外部连接会这样做吗?
    • 它不会进行更新,但可以使用相同的过程。我真的在尝试清理嵌套的 EXISTS 语句(一旦存储过程被编译,通常会作为 JOINS 处理)我赞成 Mikael Eriksson 的回答,因为他更好地保持了你的编码风格,并解释了何时以及如何进行更新
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-20
    • 2010-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多