我有一个存储过程,当运行时,从视图中初始插入aa表,然后是一个update语句,修改第一个select语句使用的视图中的表(代码如下):
ALTER PROCEDURE [dbo].[LFC_sp_LIT_PgmrRequestForm]
AS
BEGIN
SET NOCOUNT ON;
insert into firebird.helpdesk.dbo.tbl_program_request (Requester, Description, Priority, DateRequest, project_status, department, category, requester_email, DueDate, Note)
select dept_name, submitter_first_name + '-' + issue_description, severity_level, getdate(), 'Not Started', dept_name, issue_category, submitter_email, date_due, issue_details
from ics_net..CUS_CF_PGMR_REQUEST_View
where finalbalance = 0;
UPDATE ics_net..CF_Submission
SET finalbalance = 5
WHERE finalbalance = 0
AND FormId = '79A807D9-230B-494C-A609-A284DBBE41B8';
END
每当提交Web表单时都会调用此过程,目标是将数据从Web表单数据库移动到我们的帮助台数据库中。 Web表单在数据库中具有最终余额字段,该字段设置为零。移动时,我们将其更新为非零数字。
第一个语句执行会发生什么,但第二个更新永远不会发生。一旦再次调用它,第一个语句就会执行,第二个语句适用于之前应该更新的语句。我试图解决这个问题的是:
对两个语句使用ROWLOCK提示
我已确保事务隔离级别为READ COMMITTED
除了在select语句中使用NOLOCK提示之外,我无法想到其他任何事情,但我对此非常犹豫。有任何想法吗?感谢。
编辑1)基于一些讨论,我想澄清一些事情:
编辑2)更多细节
视图定义:
ALTER VIEW [dbo].[CUS_CF_PGMR_REQUEST_View] AS
select s.SubmissionId,
s.FormId,
s.SubmittedDate,
s.UserId,
s.Status,
sfn.AnswerValue as [submitter_first_name],
sub_email.AnswerValue as [submitter_email],
dname.AnswerValue as [dept_name],
d_due.AnswerValue as [date_due],
i_cat.AnswerValue as [issue_category],
sec_lev.AnswerValue as [severity_level],
i_desc.AnswerValue as [issue_description],
i_det.AnswerValue as [issue_details],
s.FinalBalance
from CF_Submissions s
left outer join CF_Answers sfn on sfn.ItemID = 'df5307ca-d9a8-4c74-955e-7fe80caafdc0'
and sfn.SubmissionID=s.SubmissionID
left outer join CF_Answers sub_email on sub_email.ItemID = '27e8e2fd-13f8-4c5a-a6bb-bc68fa7d4af5'
and sub_email.SubmissionID=s.SubmissionID
left outer join CF_Answers dname on dname.ItemID = '2826ee1b-759e-4325-b0ba-32a86ed7f07a'
and dname.SubmissionID=s.SubmissionID
left outer join CF_Answers d_due on d_due.ItemID = '5e087866-63ce-41f1-bb32-443770fdf388'
and d_due.SubmissionID=s.SubmissionID
left outer join CF_Answers i_cat on i_cat.ItemID = '32be52b1-1539-4d9b-9f7c-ffb370188396'
and i_cat.SubmissionID=s.SubmissionID
left outer join CF_Answers sec_lev on sec_lev.ItemID = 'f795ff66-342b-4155-86d6-7655dc2b10a5'
and sec_lev.SubmissionID=s.SubmissionID
left outer join CF_Answers i_desc on i_desc.ItemID = '3ff0e614-bdf6-4710-9ba0-6f7356c67032'
and i_desc.SubmissionID=s.SubmissionID
left outer join CF_Answers i_det on i_det.ItemID = 'dd5b797d-a407-4bc7-a73c-daacb3abe660'
and i_det.SubmissionID=s.SubmissionID
where s.FormId = '79a807d9-230b-494c-a609-a284dbbe41b8'
视图背后的表定义
CREATE TABLE [dbo].[CF_Answers](
[AnswerID] [uniqueidentifier] NOT NULL,
[SubmissionID] [uniqueidentifier] NULL,
[ItemID] [uniqueidentifier] NULL,
[AnswerValue] [varchar](max) NULL,
[MItemID] [uniqueidentifier] NULL,
[RowIndex] [int] NULL,
[ChildItemID] [uniqueidentifier] NULL,
[DisplayText] [varchar](max) NULL,
[NumericValue] [numeric](8, 2) NULL,
[IsVisibleOnSubmit] [bit] NULL,
CONSTRAINT [PK_CF_Answers] PRIMARY KEY CLUSTERED
(
[AnswerID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE TABLE [dbo].[CF_Submissions](
[SubmissionID] [uniqueidentifier] NOT NULL,
[UserID] [uniqueidentifier] NULL,
[FormID] [uniqueidentifier] NULL,
[SubmittedDate] [datetime] NULL,
[FinalBalance] [decimal](18, 2) NULL,
[WorkflowID] [uniqueidentifier] NULL,
[Status] [int] NULL,
[TransactionID] [varchar](50) NULL,
[WaiverCode] [varchar](max) NULL,
CONSTRAINT [PK_CF_Submissions] PRIMARY KEY CLUSTERED
(
[SubmissionID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
插入目标表的定义:
CREATE TABLE [dbo].[tbl_program_request](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Requester] [nvarchar](50) NULL,
[Description] [nvarchar](255) NULL,
[Priority] [nvarchar](10) NULL,
[DateRequest] [datetime] NULL,
[WorkingOn] [nvarchar](30) NULL,
[CompletedDate] [datetime] NULL,
[StartDate] [nvarchar](15) NULL,
[Note] [nvarchar](max) NULL,
[project_status] [nvarchar](25) NULL,
[hours_week] [decimal](4, 2) NULL,
[hours_total] [decimal](10, 2) NULL,
[department] [nvarchar](30) NULL,
[category] [nvarchar](20) NULL,
[requester_email] [nvarchar](30) NULL,
[DueDate] [datetime] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
最后,从视图中提取的一些示例数据:
SubmissionId - B81CE5F8-A87B-42B1-9339-2B36166394AB
FormId - 79A807D9-230B-494C-A609-A284DBBE41B8
已提交日期 - 1/24/2018 16:43
UserId - 58E3958A-A786-4CB9-8EDE-0837ACE187BD
状态 - 1
submitter_first_name - SM
submitter_email - email @ organization
dept_name - LIT
date_due - 1/28/2018
issue_category -MISC
severity_level - LOW
issue_description - test3
issue_details - test3
FinalBalance - 0
只是为了澄清,在SP的末尾,FinalBalance应该等于5.但是,直到第二次运行它才会设置为5,导致双重插入到tbl_program_request表中。