Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default insert multiple row

    How do i insert multiple rows in database.
    for example: I have 3 list:
    list1: a1,a2,a3,a4
    list2: b1,b2,b3,b4
    list3: c1,c2,c3,c4

    I want to insert a1,b1,c1 in 1st row. a2,b2,c2 in 2nd row,...
    How do I write one sql insert statement to do that?

    sweetyp2005 Guest

  2. Similar Questions and Discussions

    1. insert multiple row and table
      I have table1 and table 2, one to many relationship. How do I save 3 rows in table 1 and one row in table 2 at the same time?
    2. Multiple DB Insert
      I have an XML file that has roughly 55,000 records. I need to parse the contents of the file and store it in a database. The problem I'm having is...
    3. Multiple Insert or Looping Insert
      I built an application on an Access DB that allows a dispatcher to log trucks in the field at their location as they call in. There are three...
    4. Insert Multiple INTO multiple table
      Is there an extension available that can insert into multiple database tables and then retrieve the ID of the first insert? Andy
    5. multiple insert problem !
      hi ! had to come right back... this time i'm trying to do a multiple insert. in my admin website i want to add users to a specific project. i...
  3. #2

    Default Re: insert multiple row

    sweetyp2005,

    It depends on your database. As zoeski80 mentioned, mySql does allow multiple
    row inserts in a single cfquery but the syntax is not standard across most
    databases (ie. may not be portable). Access does not allow multiple row
    inserts in a single cfquery. Some other databases like sql server, etc allow
    multiple insert statements in the same query with each statement separated by a
    semi-colon.

    <cfquery ...>
    INSERT INTO someTable (column1, column2, column3)
    VALUES ('a1',b1', 'c1');
    INSERT INTO someTable (column1, column2, column3)
    VALUES ('a2',b2', 'c2');
    ... etc....
    </cfquery>

    Verify that all (3) lists contain the same number of elements and then cfloop
    from 1 to the total number of elements in the lists. Within each cfloop grab
    the list elements at the current loop index position.



    <cfset list1 = "a1,a2,a3,a4">
    <cfset list2 = "b1,b2,b3,b4">
    <cfset list3 = "c1,c2,c3,c4">

    <!--- single insert per cfquery method --->
    <cfif listlen(list1) gt 0>
    <!--- verify lists contain same number of elements --->
    <cfif listLen(list1) eq listLen(list2) AND listLen(list2) eq listLen(list3)>
    <cfloop from="1" to="#listLen(list1)#" index="j">
    <cfquery name="addData" datasource="#yourDSN#">
    INSERT INTO someTable (column1, column2, column3)
    VALUES ('#listGetAt(list1, j)#', '#listGetAt(list2, j)#', '#listGetAt(list3,
    j)#')
    </cfquery>
    </cfloop>
    <cfelse>
    Oops! The list sizes don't match.
    </cfif>
    </cfif>

    mxstu Guest

  4. #3

    Default Re: insert multiple row

    INSERT INTO someTable
    select 'a1',a2', 'a3','a4'
    union all
    select 'b1',b2', 'b3','b4'
    union all
    select 'c1',c2', 'c3','c4'





    SQLMenace 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