0

我有两个具有完全相同列的表。

两者都有称为 id 的主标识键,它会自动递增。

我的程序将数据添加到临时表中,然后过滤通过此过程添加到任务表中的内容。

我的问题是我有这个更新语句后跟一个插入,由于某种原因我的更新语句不起作用,当它本身在过程中时。

我的插入语句在过程中单独使用时有效,但在此更新语句下方时无效。

我希望我的更新声明

如果这 3 个字段在行之旅 deptdate taskname 中匹配,请更新我的dashboardtasks 表中的到期日期

如果要添加的行在这 3 个字段中不匹配,则使用下面的插入语句将该行作为新行插入。

 update dashboardtasks set
         deptdate = s.deptdate,
         tour = s.tour,
          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=dashboardtasks.tour and
          s.taskname=dashboardtasks.taskname and 
          s.deptdate=dashboardtasks.deptdate 


        insert into dashboardtasks (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 dashboardtasks as d
                          where s.tour=d.tour and
                                s.taskname=d.taskname and 
                                s.deptdate=d.deptdate and
                                s.duedate=d.duedate
                         )


id int Unchecked
tour varchar(50) Checked
taskname varchar(50) Checked
deptdate varchar(50) Checked
tasktype varchar(50) Checked
[desc] varchar(MAX) Checked
duedate varchar(50) Checked
compdate varchar(50) Checked
comments varchar(MAX) Checked
agent varchar(50) Checked
compby varchar(50) Checked
graceperiod varchar(50) Checked

这些是我的字段,但 compby、comments、compdate 和 desc 为空

4

3 回答 3

1

这是您的查询。

设置测试数据

create table dashboardtasks1(id int identity, tour int, taskname nvarchar(50), deptdate datetime, tasktype nvarchar(50), [desc] nvarchar(50), duedate datetime, compdate datetime, comments nvarchar(50), agent nvarchar(50), compby int, graceperiod int)
create table staggingtasks(id int, tour int, taskname nvarchar(50), deptdate datetime, tasktype nvarchar(50), [desc] nvarchar(50), duedate datetime, compdate datetime, comments nvarchar(50), agent nvarchar(50), compby int, graceperiod int)

insert into staggingtasks(id, tour, taskname, deptdate, tasktype, [desc], duedate, compdate, comments, agent, compby, graceperiod)
    values (62, 3647, 'Request Space', '2011-03-30', 'Land', NULL, '2010-01-06', NULL, NULL, 'PEGGYH', NULL, NULL)
insert into staggingtasks(id, tour, taskname, deptdate, tasktype, [desc], duedate, compdate, comments, agent, compby, graceperiod)
    values (81, 505, 'Rel. Space', '2012-02-22', 'Land', NULL, '2011-12-24', NULL, NULL, 'IMANA', NULL, NULL)
insert into staggingtasks(id, tour, taskname, deptdate, tasktype, [desc], duedate, compdate, comments, agent, compby, graceperiod)
    values (82, 505, 'Ticket', '2012-02-22', 'Air', NULL, '2012-01-08', NULL, NULL, 'SYLVIAT', NULL, NULL)
insert into staggingtasks(id, tour, taskname, deptdate, tasktype, [desc], duedate, compdate, comments, agent, compby, graceperiod)
    values (83, 505, 'Names to Airlines', '2012-02-22', 'Air', NULL, '2012-01-08', NULL, NULL, 'SYLVIAT', NULL, NULL)
insert into staggingtasks(id, tour, taskname, deptdate, tasktype, [desc], duedate, compdate, comments, agent, compby, graceperiod)
    values (90, 505, 'Names to Airlines', '2012-02-22', 'Air', NULL, '2012-01-01', NULL, NULL, 'SYLVIAT', NULL, NULL)
insert into staggingtasks(id, tour, taskname, deptdate, tasktype, [desc], duedate, compdate, comments, agent, compby, graceperiod)
    values (92, 505, 'Names to Airlines', '2012-02-22', 'Air', NULL, '2012-01-01', NULL, NULL, 'SYLVIAT', NULL, NULL)

insert into dashboardtasks1(tour, taskname, deptdate, tasktype, [desc], duedate, compdate, comments, agent, compby, graceperiod)
    values (3647, 'Request Space', '2011-03-30', 'Land', NULL, '2010-11-06', NULL, NULL, 'PEGGYH', NULL, NULL)
insert into dashboardtasks1(tour, taskname, deptdate, tasktype, [desc], duedate, compdate, comments, agent, compby, graceperiod)
    values (505, 'Rel. Space', '2012-02-22', 'Land', NULL, '2011-11-24', NULL, NULL, 'IMANA', NULL, NULL)
insert into dashboardtasks1(tour, taskname, deptdate, tasktype, [desc], duedate, compdate, comments, agent, compby, graceperiod)
    values (505, 'Ticket', '2012-02-22', 'Air', NULL, '2012-11-08', NULL, NULL, 'SYLVIAT', NULL, NULL)
insert into dashboardtasks1(tour, taskname, deptdate, tasktype, [desc], duedate, compdate, comments, agent, compby, graceperiod)
    values (505, 'Names to Airlines', '2012-02-22', 'Air', NULL, '2012-11-08', NULL, NULL, 'SYLVIAT', NULL, NULL)

运行更新语句

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

四行受影响。从 dashboardtasks1 中选择会为您提供此结果

id  tour    taskname    deptdate    tasktype    desc    duedate compdate    comments    agent   compby  graceperiod
1   3647    Request Space   2011-03-30 00:00:00.000 Land    NULL    2010-01-06 00:00:00.000 NULL    NULL    PEGGYH  NULL    NULL
2   505 Rel. Space  2012-02-22 00:00:00.000 Land    NULL    2011-12-24 00:00:00.000 NULL    NULL    IMANA   NULL    NULL
3   505 Ticket  2012-02-22 00:00:00.000 Air NULL    2012-01-08 00:00:00.000 NULL    NULL    SYLVIAT NULL    NULL
4   505 Names to Airlines   2012-02-22 00:00:00.000 Air NULL    2012-01-08 00:00:00.000 NULL    NULL    SYLVIAT NULL    NULL

运行插入语句

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
                 )

两行受到影响。针对dashboardtasks1 的查询会为您提供此结果。

id  tour    taskname    deptdate    tasktype    desc    duedate compdate    comments    agent   compby  graceperiod
1   3647    Request Space   2011-03-30 00:00:00.000 Land    NULL    2010-01-06 00:00:00.000 NULL    NULL    PEGGYH  NULL    NULL
2   505 Rel. Space  2012-02-22 00:00:00.000 Land    NULL    2011-12-24 00:00:00.000 NULL    NULL    IMANA   NULL    NULL
3   505 Ticket  2012-02-22 00:00:00.000 Air NULL    2012-01-08 00:00:00.000 NULL    NULL    SYLVIAT NULL    NULL
4   505 Names to Airlines   2012-02-22 00:00:00.000 Air NULL    2012-01-08 00:00:00.000 NULL    NULL    SYLVIAT NULL    NULL
5   505 Names to Airlines   2012-02-22 00:00:00.000 Air NULL    2012-01-01 00:00:00.000 NULL    NULL    SYLVIAT NULL    NULL
6   505 Names to Airlines   2012-02-22 00:00:00.000 Air NULL    2012-01-01 00:00:00.000 NULL    NULL    SYLVIAT NULL    NULL

这是预期的行为/结果吗?

于 2011-01-31T19:27:03.533 回答
0

您是否尝试过在 UPDATE 和 INSERT 语句之间添加 SELECT 语句?它可能会为您提供一些线索,说明为什么该过程未按预期工作。

此外s.duedate=d.duedate,您的 where 子句的一部分可能是多余的。

于 2011-01-31T17:10:44.113 回答
0

只是把它扔在那里,但是您是否尝试过用内部连接替换 where 子句?像这样的东西:

update dashboardtasks set
     deptdate = s.deptdate,
     tour = s.tour,
      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
    inner join dashboardtasks on
      s.tour=dashboardtasks.tour and
      s.taskname=dashboardtasks.taskname and 
      s.deptdate=dashboardtasks.deptdate 
于 2011-01-31T16:45:47.023 回答