#1
  1. No Profile Picture
    Contributing User
    ASP Explorer (0 - 99 posts)

    Join Date
    Jul 2004
    Posts
    35
    Rep Power
    13

    Loop to insert record


    Hi,

    I have to insert multiple record, the code is like this:

    while rs.eof = false
    commUpdate.CommandText = "insert into table a select.....where b.col1 = " & rs.Fields("col1").Value

    commUpdate.Execute()
    rs.movenext
    wend

    then I need to display the newly inserted record immediately.

    I would like to know if there would be any problem to insert record inside a loop and then display the record immediately. I mean would this increase the loading time of the page?
    Or is it possible that the page load before inserting the records (that means I can't see the newly inserted record)?

    Thanks!
  2. #2
  3. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Sep 2004
    Location
    CN
    Posts
    3
    Rep Power
    0

    add a response.write


    Originally Posted by deadfish
    Hi,

    I have to insert multiple record, the code is like this:

    while rs.eof = false
    commUpdate.CommandText = "insert into table a select.....where b.col1 = " & rs.Fields("col1").Value

    commUpdate.Execute()
    rs.movenext

    response.write records here

    wend

    then I need to display the newly inserted record immediately.

    I would like to know if there would be any problem to insert record inside a loop and then display the record immediately. I mean would this increase the loading time of the page?
    Or is it possible that the page load before inserting the records (that means I can't see the newly inserted record)?

    Thanks!
    add a response.write records and then through what displayed you can know which one(s) have any problem.
  4. #3
  5. No Profile Picture
    Moderator
    ASP Mastermind (5000+ posts)

    Join Date
    Feb 2004
    Location
    Reston, VA
    Posts
    13,248
    Rep Power
    1909
    I'm not sure I understand fully what you're trying to accomplish. Yes, you can loop through and insert data as needed. Once you're done inserting the records, just run a SELECT statement to display the results.
    jmurrayhead
  6. #4
  7. No Profile Picture
    Grumpier Old Moderator
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2003
    Posts
    10,143
    Rep Power
    189
    You should review your insert sql, you can't use WHERE in an insert statement.

    And for myself, I go out of my way to avoid doing db operations from within a loop.
    ======
    Doug G
    ======
    I didn't attend the funeral, but I sent a nice letter saying I approved of it. --Mark Twain
  8. #5
  9. No Profile Picture
    Moderator
    ASP Mastermind (5000+ posts)

    Join Date
    Feb 2004
    Location
    Reston, VA
    Posts
    13,248
    Rep Power
    1909
    Originally Posted by Doug G
    You should review your insert sql, you can't use WHERE in an insert statement.

    And for myself, I go out of my way to avoid doing db operations from within a loop.
    Unless using a SELECT INTO statement: http://www.w3schools.com/sql/sql_select_into.asp

    which wouldn't make sense in a looping insert.
  10. #6
  11. No Profile Picture
    Contributing User
    ASP Explorer (0 - 99 posts)

    Join Date
    Jul 2004
    Posts
    35
    Rep Power
    13
    Thanks for your reply.


    Actually I just afraid that the select statement runs before completing the insert statement. That means when I select
    records from the table, I cannot view all the newly inserted records....

    I may try to avoid the loop.....
  12. #7
  13. No Profile Picture
    Grumpier Old Moderator
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2003
    Posts
    10,143
    Rep Power
    189
    Originally Posted by jmurrayhead
    Unless using a SELECT INTO statement: http://www.w3schools.com/sql/sql_select_into.asp

    which wouldn't make sense in a looping insert.
    What does your reply have to do with the question? You can also use WHERE with an UPDATE and that wasn't asked about either.
  14. #8
  15. No Profile Picture
    Grumpier Old Moderator
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2003
    Posts
    10,143
    Rep Power
    189
    I may try to avoid the loop.....
    If possible that's my recommendation.

    You can issue another sql statement right after your insert to retrieve the newly created ID if your id column is an identity or autonumber column, code like
    Code:
    ..
    .. insert code blah blah
    set rsid = con.execute("select @@identity as MyID")
    response.write rsid("MyID")  'this should be the new ID

Similar Threads

  1. Problems reading excel files with ASP
    By fireseed in forum ASP Development
    Replies: 9
    Last Post: June 27th, 2006, 04:45 AM
  2. Replies: 10
    Last Post: March 23rd, 2006, 11:51 PM
  3. Lose record edit via form; input continues as new record
    By Karenk in forum Microsoft Access Help
    Replies: 1
    Last Post: September 23rd, 2004, 03:19 PM
  4. Retrieve Record ID after Insert
    By benno2805 in forum ASP Development
    Replies: 1
    Last Post: August 10th, 2004, 03:19 PM
  5. checking if record exists before insert or update
    By Dorothee in forum .NET Development
    Replies: 3
    Last Post: June 14th, 2004, 09:48 AM

IMN logo majestic logo threadwatch logo seochat tools logo