Question : Best way to copy SQL record using VB6

Hi Experts,
I need to be able to duplicate a record in a datatable then change one field to a different value.
I'm using this, but the process takes quite a long time.  The table is opened as shown.  This is done once at the beginning of the session.  The operator performs actions where records are added to the table.  An action may add a number of new records, and based on operator input (somewhat after the original records are created), those records may need to be duplicated.  Duplication can't be done at the same time as the creation of the records due to operator work-flow.  Duplicated records need to have one field changed to a different value.  This all works, but the .MOVENEXT command takes forever when moving past the last found record.  Also the .FILTER command takes a while to complete.  This datatable has close to 1 million records.  I have added a test before the .MOVENEXT to prevent it from trying to move past the end of the found records, but that seems "clunky".  Any thoughts?
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
Open the table:
    Set rsResponses = New ADODB.Recordset
    With rsResponses
        .CursorLocation = adUseServer
        .CursorType = adOpenForwardOnly
        .LockType = adLockOptimistic
        .Open sCont, dbData, , , adCmdText
    End With

Duplicate the found records, changing the ProfID field:

    sCont = "ProfID = " & lOrgID
    With rsResponses
        .Filter = sCont
        Do While Not .BOF And Not .EOF
            lQuestID = .Fields("QuestID")
            lAnsID = .Fields("AnsID")
            sComments = .Fields("Comments")
            sGC = .Fields("GroupCode")
            
            sCont = "SET NOCOUNT ON; INSERT INTO " & sTDResponses & " (QuestID,MagID,ProfID,AnsID,Comments,GroupCode)" _
                & " VALUES (" & lQuestID & "," & lMagID & "," & lNewID & "," & lAnsID & "," _
                & "'" & sComments & "','" & sGC & "')"
            Set rsTemp = dbData.Execute(sCont)
            Set rsTemp = Nothing
            .MoveNext
        Loop
        .Filter = ""
    End With

Answer : Best way to copy SQL record using VB6

OK - it does not matter that the source and destination are the same.  

insert into myTable
(QuestId, MagId, ProfId, AnsId, Comments, GroupCode)
Select QuestId, MagId, 'new value for ProfId' as ProfId, AnsId, Comments, GroupCode
from
myTable T2
where T2.IsNew = 1    -- obviously, subsitute your own logic in the where clause

If you simply execute this against the server rather than bringing everything locally and iterating through each record it will be MUCH faster


Random Solutions  
 
programming4us programming4us