Results 1 to 4 of 4

Thread: INSERT SQLString

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2004
    Posts
    16

    INSERT SQLString

    Hi

    Im trying to INSERT 152 rows in a table. To speed it up im trying to build a String with all the rows and then execute them all at once. Im using ADODB.Command but it looks like I can only insert one row at a time. Does anyone have an idea how to do this?

    This is my code(Automation error):

    szSQLString = ""
    i = 0

    Do While Not oRs2.EOF

    szSQLString = szSQLString + "INSERT INTO...."

    i = i + 1

    If i = 152 Then

    objCommand.CommandText = szSQLString

    objCommand.Execute RecordsAffected:=lRecordsAffected, Options:=adCmdText Or adExecuteNoRecords

    i = 0
    szSQLString = ""

    End If

    oRs2.MoveNext
    Loop


    Best regards

    /Bjso

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: INSERT SQLString

    Can't say I understand that code, but gave it code tags:
    VB Code:
    1. szSQLString = ""   'this is unnecessary
    2. i = 0
    3.  
    4. Do While Not oRs2.EOF
    5.    'this will make one string with 152 INSERT statements
    6.    szSQLString = szSQLString + "INSERT INTO...."  'are you leaving out the rest of the SQL?
    7.    i = i + 1
    8.  
    9.    If i = 152 Then
    10.       objCommand.CommandText = szSQLString
    11.       objCommand.Execute RecordsAffected:=lRecordsAffected, Options:=adCmdText Or adExecuteNoRecords
    12.       i = 0
    13.       szSQLString = ""
    14.    End If
    15.  
    16.    oRs2.MoveNext
    17. Loop
    SQL Insert syntax is:
    Code:
    INSERT INTO tblFoo (fld1, fld2,...fldn) VALUES (str1, str2,...strn)
    'WHERE clause optional
    WHERE fld1 = 'Booga Booga'
    It looks like you're building a long string with 152 INSERT INTO statements, with no fields or values, although maybe you just left those out. If so, I don't see where you're giving them unique values for each record.
    If unique values for each record are what you want, you'd need to execute 152 separate INSERT statements. I've never heard of multiple INSERT statements in one query, and it would have to be a big performance hit to execute a bunch of single statements in a row. If your data is in a file, you could just read that in, or since it seems your data comes from a recordset, you could make the query used in the recordset part of the INSERT statement instead.
    If you don't want unique values (uncommon, but maybe n fields in each record have generic data), a plain INSERT statement would work.
    Tengo mas preguntas que contestas

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2004
    Posts
    16

    Re: INSERT SQLString

    Sorry, a bit confusing code.

    I have a query which contains a large number of records (oRs2). which I want to insert to the db.

    If I execute after every record it works fine, but it takes ages(se below)

    Ex1:

    Do While Not oRs2.EOF

    objCommand.CommandText = "INSERT INTO Prognosis(PROJECT_ID,NAME,ACTIVITY_NO,DESCRIPTION,ACCOUNT_DATE,INTERNAL_QUANTITY,TYPE) " & _
    "VALUES('" & oRs2.Fields(0).Value & "','" & oRs2.Fields(1).Value & "','" & oRs2.Fields(2).Value & "','" & oRs2.Fields(3).Value & "','" & oRs2.Field (4).Value & "','0','Upparbetad'); "

    objCommand.Execute RecordsAffected:=lRecordsAffected, Options:=adCmdText Or adExecuteNoRecords

    oRs3.MoveNext
    Loop



    To improve the performance I'm therefore trying to build a long string and execute when it gets to 152 (Someone told me building a string like this will speed up the insert process)Therfore I wrote this(se below), however its not working, it says there's something wrong at the end of the query. So I wonder if this is actually possible to do in VBA(excel) and then insert into ACCESS db. Or if I have to write the code in another way?

    My code:

    i = 0
    Do While Not oRs2.EOF

    szSQLString = szSQLString + "INSERT INTO Prognosis(PROJECT_ID,NAME,ACTIVITY_NO,DESCRIPTION,ACCOUNT_DATE,INTERNAL_QUANTITY,TYPE) " & _
    "VALUES('" & oRs2.Fields(0).Value & "','" & oRs2.Fields(1).Value & "','" & oRs2.Fields(2).Value & "','" & oRs2.Fields(3).Value & "','" & oRs2.Field (4).Value & "','0','Upparbetad'); "

    i = i + 1

    If i = 152 Then

    objCommand.CommandText = szSQLString
    objCommand.Execute RecordsAffected:=lRecordsAffected,Options:=adCmdText Or adExecuteNoRecords
    i = 0
    szSQLString = ""

    End If

    oRs2.MoveNext
    Loop

    Best regards

    /Bjso

  4. #4
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: INSERT SQLString

    uh ... if you are reading from a db table use the insert into sql statement on that table to the destination table.

    Use the Access query builder for an example.
    Open the query (or make one) that retrieves the data you want
    Change the type to insert (drop down type list on a toolbar or via menu).
    It will ask you for the destination table (which I guess you have already?) Pick it.
    Then for each field, there is a dropdown choice of destination fields now on a new row displayed in the query builder.
    Match the fields up.
    If you want to see what this is in an Sql statement, then change the view to Sql and see how it can be done...

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width