I have two tables with the exact same columns.
Both have primary identity keys called id that auto increment.
My program adds data to a stagging table and then filters what gets added to the task table through this procedure.
Mmy problem is
I have this update statement followed by an insert,
for some reason my update statement does not work, when it is in the procedure by itself.
My insert statement works when it is by itself in the procedure but not when underneath this update statement.
I want my update statement to
Update my duedate in my dashboardtasks table,
if these 3 fields match in the row
tour
deptdate
taskname
If the row being added does not match in these 3 fields than insert the row as a new row using my insert statement underneath.
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
these are my fields, but compby, comments, compdate, and desc are null
Here is a run through with your queries.
Set up test data
Run the update statement
Four rows affected. A select from dashboardtasks1 gives you this result
Run the insert statement
Two rows are affected. A query against dashboardtasks1 gives you this result.
Is this the expected behavior/result?