访问列表框查询阻止SQL Server更新查询



我正在开发一个访问应用程序和SQL Server后台同时。我有一个表单与列表框,其中,当记录被双击,打开一个未绑定的表单,并根据所选的记录加载数据到它。当在第二种表单中进行更改时,按钮启动一个传递查询,该查询执行一个存储过程,更新SQL Server中基表中记录的详细信息。

是这样的。只要打开Form1(带有列表框),存储过程就会超时而不运行。如果我关闭这个表单,不到一秒钟就完成了。当从Access中运行时,当从management studio中运行时,以及当在management studio中作为带硬值的查询(而不是带参数的sproc)运行时,它的行为都是这样的。

列表框的行源是一个引用SQL Server中的视图的链接表。视图中的查询是两个不同表的递归公共表表达式,其中一个表是由进程编辑的表。我将视图设置为只读。还有什么我能帮上忙的吗?

下面是存储过程:

PROCEDURE [dbo].[spSalesPlanUpdate]
@Salesyear numeric(4,0),
@ItemNumber varchar(20),
@Baseline int,
@Speculation int,
@Comments varchar(max)
AS
declare @SY numeric(4,0),
@ItN varchar (20),
@BL int,
@SPL int,
@CmT varchar(max)
set @SY = @Salesyear
set @ItN = @ItemNumber
set @BL = @Baseline
set @SPL = @Speculation
set @CmT = @Comments
BEGIN
SET NOCOUNT ON;

update SalesPlan
set Baseline = @BL
,Speculation = @SPL
,DateModified = getdate()
,Comments = @CmT
where SalesYear = @SY and ItemNumber = @ItN
END

我同时使用了参数和局部变量,因为一开始我认为这可能是关于参数嗅探。

查看列表框的视图:

view [dbo].[vwSalesPlan] as 
with cte 
as
(
select Item, year(getdate()) as SY
from vwItemsAndLiners il
union all
select ial.Item, 
(cte.SY + 1)
From vwItemsAndLiners ial join cte on ial.Item  = cte.Item
Where SY < (year(getdate())+ial.YearsFromProp)
)
select sp.ItemNumber, ial.Variety, ial.Size, ial.PerTray, sp.SalesYear, sp.SalesYear - ial.YearsFromProp as PropYear, 
sp.SalesYear - ial.YearsFromProduction as ProductionYear, 
sp.Baseline, sp.Speculation, 
CEILING((CAST(SP.BASELINE AS NUMERIC (12,2)) + CAST(SP.SPECULATION AS numeric(12,2)))/IAL.PerTray)*IAL.PerTray as Total , 
sp.DateModified, ial.Segment ,'Entered' as [Status], sp.Comments
From SalesPlan sp inner join vwItemsAndLiners ial on sp.ItemNumber = ial.Item
Where ial.status = 'Sell'

union 
select cte.Item, ial.Variety, ial.Size, ial.PerTray, SY, cte.sy - ial.YearsFromProp as PropYear, 
cte.SY - ial.YearsFromProduction as ProductionYear,'', '', 0, null, ial.Segment , 'Not Entered', null
from cte inner join vwItemsAndLiners ial on cte.Item = ial.Item
where cte.Item not in (select ItemNumber from SalesPlan where salesplan.SalesYear = CTE.SY) and ial.Status = 'Sell'
with check option

正在更新的表:SalesPlan

查看从以下命令查询列表框:vwSalesPlan

我意识到这里有很多东西。真的,我只是希望这产生一些想法,为什么一个表单被打开会锁定原始表从更新查询。谢谢!

我试过了:

  • 索引SQL Server中为列表框提供行源的视图,但由于它们包含CTE,因此无法被索引。
  • lstbox.recordset。movefirst然后lstbox.recordset. moveast强制访问读取整个列表,但每当列表被过滤或要求时,它会抛出一个错误,说recordset对象已经更改,movefirst命令无效。

所以我写了这个sub:

Private Sub readtheData()
Dim i As Integer
i = Me.lstSalesPlan.ListCount
End Sub

只是强制它在每次加载表单或过滤列表框后面的查询时计数记录,强制访问以释放锁。希望这对将来的人有所帮助!

最新更新