I have this script which allows me to import data from CSV and insert into SQL (credit - JordanCClark). Imports typically have ~1000 rows. Following the import I have a SQL while loop which goes back through and updates the SortOrder. This update is inefficient and is causing delays in processing, and contributing to the occasional deadlock. I would like to be able to accomplish this in the original insert, but don’t know how to do that. Any help is greatly appreciated.
[code]event.source.parent.Loading = 1
wcid = event.source.parent.WCID
changeby = system.tag.getTagValue("[Client]OverrideUsername")
import fileinput
inputFile=system.file.openFile(‘csv’)
if inputFile != None:
fileinput.close()
values = “”
flag = 0
for lineIn in fileinput.input(inputFile):
flagTest=lineIn[:-(len(lineIn)-5)] #Extract first five characters of the line.
if flag==1:
columns=’(’ + lineIn + ‘)’ #column list for query
flag=0 #Reset flag
if flag==2:
values+="(’" + lineIn.replace(’"’,’’).replace(",","’,’") + “’),” #add set of values. Strip away all double quotes,
# add single quotes to all values,
# wrap in parentheses,
# and pop a comma on the end.
# The comma prepares for the next set of values.
if flagTest ==’#NAME’: #Set flag according to '#" line delimiter
flag=1
elif flagTest == ‘#ROWS’:
flag=2
elif flagTest == ‘#TYPE’:
flag=0
fileinput.close()
values=values[:-1] #Strip off last comma after all the values have been processed
query=“INSERT INTO tblOMS_AX " + columns + " VALUES “+ values
query = query.replace(”\n”,"").replace("\r","")
print query
system.db.runUpdateQuery(query)
sql = “UPDATE tblOMS_AX SET WCID=?, ChangeBy=?, ChangeDate=GETDATE() WHERE WCID IS NULL”
system.db.runPrepUpdate(sql, [wcid, changeby])
sql = “”“DECLARE @mycount INT
SET @mycount = (SELECT count(*) FROM tblOMS_AX WHERE SortOrder IS NULL AND WCID = ?)
WHILE @mycount > 0
BEGIN
UPDATE TOP (1) tblOMS_AX SET SortOrder=(SELECT ISNULL(MAX(SortOrder),0) + 1 FROM tblOMS_AX WHERE WCID = ?) WHERE WCID=? AND SortOrder IS NULL
SET @mycount = @mycount - 1
END”""
system.db.runPrepUpdate(sql, [wcid, wcid, wcid])
system.db.refresh(event.source.parent,“Data”)
event.source.parent.Loading = 0[/code]
Import format.csv (479 Bytes)