[英]SQL conversion script causes System.OutOfMemoryException
When I run the script below, I get this exception. 当我在下面运行脚本时,出现此异常。 It crashed after about 20 minutes (+/- a couple minutes).
它在大约20分钟(+/-几分钟)后崩溃。 How can I prevent this exception without creating batches?
如何在不创建批处理的情况下防止此异常? I want to do this for all client records that need it.
我想对所有需要它的客户记录执行此操作。
Exception: 例外:
An error occurred while executing batch. Error message is: Exception of type 'System.OutOfMemoryException' was thrown.
SQL conversion script: SQL转换脚本:
SELECT clientid
INTO #Temp
FROM client
DECLARE
@iteratorId INT,
@phone NVARCHAR(64),
@fax NVARCHAR(64),
@contactid INT
WHILE (SELECT Count(*) FROM #Temp) > 0
BEGIN
SELECT TOP 1 @iteratorId = clientid FROM #Temp
SET @contactid = NULL
--try and grab the first non null phone number in priority order
SET @phone = ISNULL((
SELECT TOP 1
dayphone
FROM contact c
INNER JOIN dbo.clientcontact cc ON c.contactid = cc.contactid
WHERE
clientid = @iteratorId
AND cc.contacttypeid=2
AND c.dayphone IS NOT NULL
ORDER BY cc.parentclientcontactid, priority DESC
),'')
--try and grab the first non null fax priority order
SET @fax = ISNULL((
SELECT TOP 1
fax
FROM contact c
INNER JOIN dbo.clientcontact cc ON c.contactid = cc.contactid
WHERE
clientid = @iteratorId
AND cc.contacttypeid=2
AND c.fax IS NOT NULL
ORDER BY cc.parentclientcontactid, priority DESC
),'')
IF NOT EXISTS(SELECT * FROM clientcontact WHERE clientid=@iteratorId AND contacttypeid=3 AND priority=1)
BEGIN
INSERT INTO dbo.contact
( versionnumber ,
createdate ,
firstname ,
lastname ,
title ,
middleinitial ,
dayphone ,
nightphone ,
mobilephone ,
fax ,
securefax ,
extranetusername ,
extranetpassword ,
email ,
active ,
testfaxpagereceived ,
ftpoptionid ,
testresultnotify
)
VALUES ( 0 , -- versionnumber - int
GETDATE() , -- createdate - datetime
NULL , -- firstname - nvarchar(64)
NULL , -- lastname - nvarchar(64)
NULL , -- title - nvarchar(64)
NULL , -- middleinitial - nvarchar(64)
NULL , -- dayphone - nvarchar(64)
NULL , -- nightphone - nvarchar(64)
NULL , -- mobilephone - nvarchar(64)
NULL , -- fax - nvarchar(64)
'' , -- securefax - bit
NULL , -- extranetusername - nvarchar(64)
NULL , -- extranetpassword - nvarchar(64)
NULL , -- email - nvarchar(127)
0 , -- active - bit
0 , -- testfaxpagereceived - bit
NULL , -- ftpoptionid - int
NULL -- testresultnotify - bit
)
SET @contactid = @@IDENTITY
EXEC dbo.procContactSave
@contactid = @contactid, -- int
@firstname = "General", -- nvarchar(64)
@middleinitial = NULL, -- nvarchar(64)
@nightphone = NULL, -- nvarchar(64)
@lastname = "Info", -- nvarchar(64)
@email = NULL, -- nvarchar(127)
@fax = @fax, -- nvarchar(64)
@testfaxpagereceived = 0, -- bit
@mobilephone = NULL, -- nvarchar(64)
@securefax = 0, -- bit
@active = 1, -- bit
@extranetpassword = NULL, -- nvarchar(64)
@extranetusername = NULL, -- nvarchar(64)
@title = NULL, -- nvarchar(64)
@ftpoptionid = NULL, -- int
@dayphone = @phone, -- nvarchar(64)
@testresultnotify = 0, -- bit
@audituserid = 0 -- int
EXEC dbo.procClientContactSave
@clientcontactid = NULL, -- int
@priority = 1, -- int
@clientid = @iteratorId, -- int
@contacttypeid = 3, -- int
@contactid = @contactid, -- int
@audituserid = 0 -- int
END
DELETE #Temp Where clientid = @iteratorId -- delete row so we don't loop over it again
END
DROP TABLE #Temp
In the 2 stored procedures mentioned in my comments, I was calling standard web application stored procedures, which inserted a record, but also returned that record in a result set (with just the 1 record) for each iteration in the script. 在我的注释中提到的2个存储过程中,我正在调用标准的Web应用程序存储过程,该存储过程插入了一条记录,但是还针对脚本中的每次迭代在结果集中返回了该记录(只有1条记录)。 The script ran for 1.5 minutes when I commented the SELECT statement in those 2 stored procedures, so I imagine it was crashing from the amount of data SSMS was displaying from those result sets.
当我在这2个存储过程中注释SELECT语句时,该脚本运行了1.5分钟,因此我想象它因这些结果集中SSMS显示的数据量而崩溃。 That prevented the memory crash, not to mention the reduction in time it took to run (from 20+ min to 1.5 min).
这样可以防止内存崩溃,更不用说运行时间的减少了(从20分钟以上减少到1.5分钟)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.