1

I have an Access2003 database which has a table linked in from a SQL Server database. I have this Access vba that updates a column in that SQL Server table. Each month that table gets cleared and a new set of records is imported, the amount of records ranges from 300,000 to 450,000 each monthly.

This Access vba code runs but takes 5+ hours. I need to do something about that, anyone have any ideas of how to speed this up? One idea I had was to convert this to a stored procedure on the SQL Server but I have no idea how to write that so any help is appreciated.

Thanks

bobh.

VBA code:

' update the ID field in the import table 
Dim intCounter As Double 
Set MyRec = MyDB.OpenRecordset("tblMarsImport", dbOpenDynaset, dbSeeChanges) 

MyRec.MoveFirst 
intCounter = Format(Date, "yyyymmdd") & "000001" 

Do While Not MyRec.EOF 
   MyRec.Edit 
   MyRec!MarsID = CStr(intCounter) 
   MyRec.Update 
   MyRec.MoveNext 
   intCounter = intCounter + 1 
Loop 

MyRec.Close 
4

2 回答 2

0
DECLARE @Result int
SELECT @Result = (SELECT SUM (row_count)
FROM sys.dm_db_partition_stats 
WHERE object_id=OBJECT_ID('tblMarsImport')    
AND (index_id=0 or index_id=1))
SELECT @Result

SET I=1
REPEAT
Update tblMarsImport set MarsID = Cast(Replace(Convert(varchar(10), Date, 120), '-', '') As int)
 + RIGHT('00000' + I, 6)

  SET I=I+1
UNTIL I>@Result

You may need to tweak the date a bit, but that should get you really close.

EDIT: On second thought, you'll need an Identity column in your table (similar to an AutoNumber field in Access) so you can select the ID of the record you're updating by using a WHERE clause in the Update command. Otherwise, it'll just keep updating the same record over and over.

于 2013-06-14T19:52:51.443 回答
0

您可能需要查看 Row_number 以跟踪您当前正在循环的行。这样您就不需要向表中添加新列。[行号文档][1] https://msdn.microsoft.com/en-us/library/ms186734.aspx

于 2017-03-09T21:44:59.220 回答