【发布时间】: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