现在我将光标更新为2个表,并根据特定的select语句插入一个表中,此select语句返回2列x,yi需要x在表“PX”中更新,因为x是此表中的主键,需要x在表“FX”中更新,因为x是此表中的外键,然后插入第三个表x数据。
我需要更改此光标并使用我尝试的更新和插入脚本,但我发现我需要循环才能实现我的目标,所以如果有人知道我是否可以更改此光标。
并提前致谢
DECLARE @id int
DECLARE @clientid uniqueidentifier
DECLARE @code int
DECLARE @Wtime int
DECLARE @closeComplaint CURSOR
SET @closeComplaint = CURSOR FAST_FORWARD
FOR
SELECT ComplaintId, [ClientId]
FROM complaint
WHERE ComplaintStatusId = 5
AND (waitingForCutomerCloseDateTime < GETDATE() OR
waitingForCutomerCloseDateTime = GETDATE())
OPEN @closeComplaint
FETCH NEXT FROM @closeComplaint INTO @id, @clientid
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT
waitingForCutomerCloseTime = @Wtime
FROM
SystemConfiguration
WHERE
ClientId = @clientid
SELECT
[Code] = @code
FROM
[dbo].[resp_users]
WHERE
ClientId = @clientid
UPDATE activity
SET ActivityStatus = 4,
CompletionDate = GETDATE(),
ClosedBy = @code
WHERE [ComplaintId] = @id
UPDATE [dbo].[Complaint]
SET ComplaintStatusId = 2
WHERE [ComplaintId] = @id
INSERT INTO [dbo].[Note] ([Note_Description], [ClientId], [User_Code], [Visible_Internal],
[ComplaintId], [Note_DateTime], [ComplainantId],
[OneStopDesk_CustomerEmail], [OneStopDesk_CustomerUsername], [Private])
VALUES (N'Automatically closed by system after ' + @Wtime, @clientid, @code, 1,
@id, GETDATE(), null, null, null, 1)
FETCH NEXT FROM @closeComplaint INTO @id, @clientid
END
CLOSE @closeComplaint
DEALLOCATE @closeComplaint
答案 0 :(得分:0)
我不是完全确定我把一切都搞定了(你没有发布表格结构,所以我真的只能猜测有时这些表是如何连接的) - 但你应该能够基本上在 3个简单的,基于集合的语句中完成所有这些 - 并且应该比光标更快 LOT !
-- declare table variable
DECLARE @Input TABLE (CompaintID INT, ClientID INT)
-- save the rows into a table variable
INSERT INTO @Input (ComplaintID, ClientID)
SELECT ComplaintID, ClientID
FROM dbo.Complaint
WHERE ComplaintStatusId = 5
AND waitingForCustomerCloseDateTime <= GETDATE()
UPDATE a
SET ActivityStatus = 4,
CompletionDate = GETDATE(),
ClosedBy = u.Code
FROM dbo.Activity a
INNER JOIN @Input i ON a.ComplaintId = i.ComplaintId
INNER JOIN dbo.resp_users u ON i.ClientId = u.ClientId
UPDATE dbo.Complaint
SET ComplaintStatusId = 2
WHERE
ComplaintStatusId = 5
AND waitingForCustomerCloseDateTime <= GETDATE()
INSERT INTO dbo.Note ([Note_Description], [ClientId], [User_Code], [Visible_Internal],
[ComplaintId], [Note_DateTime], [ComplainantId],
[OneStopDesk_CustomerEmail], [OneStopDesk_CustomerUsername], [Private])
SELECT
N'Automatically closed by system after ' + sc.waitingForCustomerCloseTime,
i.ClientId, u.Code, 1,
i.ComplaintId, GETDATE(), null, null, null, 1
FROM
@Input i
INNER JOIN
dbo.SystemConfiguration sc ON i.ClientId = sc.ClientId
INNER JOIN
dbo.resp_user u ON u.ClientId = i.ClientId