我有两张完全相同的列。
两者都有名为id的主要身份密钥,自动递增。
我的程序将数据添加到不稳定的表中,然后通过此过程过滤添加到任务表的内容。
妈咪的问题是 我有这个更新语句后跟一个插入, 由于某种原因,我的更新语句不起作用,当它在程序中时它自己。
我的插入语句在程序中单独使用,但在此更新语句下面时则不起作用。
我想要更新语句
在我的dashboardtasks表中更新我的duedate, 如果这3个字段在行中匹配 游览 deptdate TASKNAME
如果要添加的行在这3个字段中不匹配,则使用下面的insert语句将行插入新行。
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为null
答案 0 :(得分: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语句
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
这是预期的行为/结果吗?
答案 1 :(得分: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
答案 2 :(得分:0)
您是否尝试在UPDATE和INSERT语句之间插入SELECT语句?它可能会为您提供一些线索,说明为什么程序无法按预期运行。
此外,where子句的s.duedate=d.duedate
部分可能是多余的。