我有以下作业表,其中作业将每28天轮换一次。
添加新客户后,我将首先手动创建一个作业。
我想将Complete列更新为true,并将新记录插入到同一个表中,新作业仅使用新日期(28天后)指定完全相同的详细信息,其中Complete和Paid值为false 。
我尝试使用FOR UPDATE触发器失败,并且更喜欢使用存储过程。
如何做到这一点?
CREATE TABLE [dbo].[Jobs](
[JobID] [int] IDENTITY(1,1) NOT NULL,
[CustomerID] [int] NOT NULL,
[JobDate] [date] NOT NULL,
[Price] [decimal](7, 2) NOT NULL,
[Complete] [bit] NOT NULL,
[Paid] [bit] NOT NULL,
CONSTRAINT [PK_Jobs] PRIMARY KEY CLUSTERED
CONSTRAINT [FK_CustomerID] FOREIGN KEY([CustomerID])
REFERENCES [dbo].[Customer] ([CustomerId])
答案 0 :(得分:2)
尝试更新现有记录并根据它创建一个新记录:
CREATE PROC MyProc
@CustomerID int
AS
DECLARE @JobID int;
SELECT TOP 1 @JobID = JobID
FROM Jobs WHERE CustomerID = @CustomerID AND Complete = 0
ORDER BY JobDate DESC;
UPDATE Jobs
SET Complete = 1
WHERE JobID = @JobID;
INSERT Jobs(CustomerID, JobDate, Price, Complete, Paid)
SELECT CustomerID, DATEADD(m,1,JobDate), Price, 0, 0
FROM Jobs WHERE JobID = @JobID;