inserting into multiple rows with one call

Ask a Question related to ASP Database, Design and Development.

  1. #1

    Default inserting into multiple rows with one call

    I have had considerable trouble adding multiple rows into a MSSQL7 database.

    I have tried looping through an array to create the SQL string, followed by
    adding one long SQL query.

    I have had no success with either method. The only joy is that the first
    record is added successfully. If anyone can recommend any pointers I would
    be very grateful.

    All the variables are being passed correctly and everything works fine if
    there is only one record to be added.

    for i = lbound(array) to ubound(array)

    sql = sql & "INSERT INTO TABLE_NAME (Var1,Var2,Var3,Var4,Var5,Var6,Var7)
    VALUES ("
    sql = sql & "'" & var1 & "',"
    sql = sql & "'" & var2 & "',"
    sql = sql & "'" & var3 & "',"
    sql = sql & "'" & var4 & "',"
    sql = sql & "'" & var5 & "',"
    sql = sql & "'" & var6 & "',"
    sql = sql & "'" & var7 & "');"
    Next
    conn.Execute sql

    Thanks,
    Andy


    Andy Smith Guest

  2. Similar Questions and Discussions

    1. Inserting Multiple Rows
      Is it possible to insert a number of rows into a table based on a value entered in a textbox? For example (I'm a tech writer not a...
    2. Inserting rows into a DB
      I have heard that the most time consuming task into a DB is the when we use the Insert Statatement, Is that true? Ok I have an application...
    3. SQL0954C: application heap shortage inserting rows
      Hello, I have got some probems with APPLHEAPSZ DB parameter on DB2 v8.1.x. I try to make mass inserts into a table with a column specified as...
    4. using :new / :old values in a trigger (after inserting rows)
      I am trying run an AFTER INSERT trigger that reads both :new and :old values corresponding to some of the columns of the record I am trying to...
    5. Inserting into a set of rows in an SPL
      Adam Williams wrote in message ... You need to give it a name: DEFINE matrix MULTISET(...); INSERT INTO TABLE(matrix) VALUES (...
  3. #2

    Default Re: inserting into multiple rows with one call

    What happens when you do:

    for i = lbound(array) to ubound(array)

    sql = sql & "INSERT INTO TABLE_NAME (Var1,Var2,Var3,Var4,Var5,Var6,Var7)
    VALUES ("
    sql = sql & "'" & var1 & "',"
    sql = sql & "'" & var2 & "',"
    sql = sql & "'" & var3 & "',"
    sql = sql & "'" & var4 & "',"
    sql = sql & "'" & var5 & "',"
    sql = sql & "'" & var6 & "',"
    sql = sql & "'" & var7 & "');"
    Next
    '''''''''''''''conn.Execute sql
    response.write sql

    Copy and paste that into query analyzer and execute.

    And in your sample code, var1, var2, etc. are arrays also, or are you just
    inserting the same duplicate data ubound(array) + 1 times?

    Ray at home


    --
    Will trade ASP help for SQL Server help


    "Andy Smith" <someone@microsoft.com> wrote in message
    news:%23qgRuqHXDHA.608@TK2MSFTNGP12.phx.gbl...
    > I have had considerable trouble adding multiple rows into a MSSQL7
    database.
    >
    > I have tried looping through an array to create the SQL string, followed
    by
    > adding one long SQL query.
    >
    > I have had no success with either method. The only joy is that the first
    > record is added successfully. If anyone can recommend any pointers I would
    > be very grateful.
    >
    > All the variables are being passed correctly and everything works fine if
    > there is only one record to be added.
    >
    > for i = lbound(array) to ubound(array)
    >
    > sql = sql & "INSERT INTO TABLE_NAME (Var1,Var2,Var3,Var4,Var5,Var6,Var7)
    > VALUES ("
    > sql = sql & "'" & var1 & "',"
    > sql = sql & "'" & var2 & "',"
    > sql = sql & "'" & var3 & "',"
    > sql = sql & "'" & var4 & "',"
    > sql = sql & "'" & var5 & "',"
    > sql = sql & "'" & var6 & "',"
    > sql = sql & "'" & var7 & "');"
    > Next
    > conn.Execute sql
    >
    > Thanks,
    > Andy
    >
    >

    Ray at Guest

  4. #3

    Default Re: inserting into multiple rows with one call

    I get the following sql query

    INSERT INTO ORDER_INFO (Var1,Var2,Var3,Var4,Var5,Var6,Var7) VALUES
    ('1','PID01','15.00','1','M','SID','BID');INSERT INTO ORDER_INFO
    (Var1,Var2,Var3,Var4,Var5,Var6,Var7) VALUES
    ('2','PID06','15.00','1','M','SID','BID');

    Andy


    Andy Smith Guest

  5. #4

    Default Re: inserting into multiple rows with one call

    And you can successfully execute that in query analyzer?

    Are your columns really named var1, var2, etc.?

    Ray at home

    --
    Will trade ASP help for SQL Server help


    "Andy Smith" <someone@microsoft.com> wrote in message
    news:OG93L9HXDHA.2516@TK2MSFTNGP09.phx.gbl...
    > I get the following sql query
    >
    > INSERT INTO ORDER_INFO (Var1,Var2,Var3,Var4,Var5,Var6,Var7) VALUES
    > ('1','PID01','15.00','1','M','SID','BID');INSERT INTO ORDER_INFO
    > (Var1,Var2,Var3,Var4,Var5,Var6,Var7) VALUES
    > ('2','PID06','15.00','1','M','SID','BID');
    >
    > Andy
    >
    >

    Ray at Guest

  6. #5

    Default Re: inserting into multiple rows with one call

    No my variables are not var1 etc. I wrote the loop for the e-mail. my sql
    query uses cookies, session variables and response.form values so it was
    neater to re-write it.

    My SQL output is:

    INSERT INTO ORDER_INFO (OrderID, ProductID, UnitPrice, Quantity, Size,
    ShopID, BandID) VALUES ('60', 'META03', '18.00', '1', 'M', 'BSI', 'META');
    INSERT INTO ORDER_INFO (OrderID, ProductID, UnitPrice, Quantity, Size,
    ShopID, BandID) VALUES ('60', 'IRON07', '18.00', '1', 'M', 'BSI', 'IRON');

    I have posted this into my query analyzer and I successfully add the second
    row, but it appears the first record was overwritten.

    Andy



    Andy Smith Guest

  7. #6

    Default Re: inserting into multiple rows with one call

    "Andy Smith" <someone@microsoft.com> wrote in message
    news:RsiYa.2807$0p2.677@news-binary.blueyonder.co.uk...
    >
    > INSERT INTO ORDER_INFO (OrderID, ProductID, UnitPrice, Quantity, Size,
    > ShopID, BandID) VALUES ('60', 'META03', '18.00', '1', 'M', 'BSI', 'META');
    > INSERT INTO ORDER_INFO (OrderID, ProductID, UnitPrice, Quantity, Size,
    > ShopID, BandID) VALUES ('60', 'IRON07', '18.00', '1', 'M', 'BSI', 'IRON');
    >
    > I have posted this into my query analyzer and I successfully add the
    second
    > row, but it appears the first record was overwritten.
    Um, are you sure about that? Even if you do a select * afterwards in QA,
    you do not see both records? Do you have any triggers setup that, I don't
    know, delete records with the same OrderID, or anything odd like that?

    What happens if you execute that same query but by disabling any triggers
    that may exist using this method.
    [url]http://www.aspfaq.com/2016[/url]

    Ray at home


    Ray at Guest

  8. #7

    Default Re: inserting into multiple rows with one call

    I've solved the problem...

    I reverse coded my query analyzer input and successfully added multiple
    records.

    Thanks for your help

    Andy


    Andy Smith Guest

  9. #8

    Default Inserting into multiple rows with one call

    Could you please post the solution that you came up with as I am having the same issue. Thanks.
    Unregistered Guest

Posting Permissions

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

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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139