Insert multiple rows using one insert statement in Access 2010

后端 未结 8 494
醉酒成梦
醉酒成梦 2020-12-17 22:19

I want to insert multiple values into an Access 2010 table, but I can\'t seem to find a way.
MySQL had a nice way:

INSERT INTO Production.UnitMeasure
VAL         


        
相关标签:
8条回答
  • 2020-12-17 23:05

    MS Access does not allow multiple insert from same sql window. If you want to insert, say 10 rows in table, say movie (mid, mname, mdirector,....), you would need to open the sql windows,

    1. type the 1st stmt, execute 1st stmt, delete 1st stmt
    2. type the 2nd stmt, execute 2nd stmt, delete 2nd stmt
    3. type the 3rd stmt, execute 3rd stmt, delete 3rd stmt ......

    Very boring. Instead you could import the lines from excel by doing:

    1. Right-click on the table name that you have already created
    2. Import from Excel (Import dialog box is opened)
    3. Browse to the excel file containing the records to be imported in the table
    4. Click on "Append a copy of the records to the table:"
    5. Select the required table (in this example movie)
    6. Click on "OK"
    7. Select the worksheet that contains the data in the spreadsheet
    8. Click on Finish

    The whole dataset in the excel has been loaded in the table "MOVIE"

    0 讨论(0)
  • 2020-12-17 23:11

    As marc_s has pointed out, for SQL Server 2008 and later, you can just use table value constructors. For previous versions, you can use insert and select...union all, e.g.:

    INSERT INTO Production.UnitMeasure
    SELECT N'FT2',N'Square Feet ','20080923' union all
    SELECT N'Y',  N'Yards',       '20080923' union all
    SELECT N'Y3', N'Cubic Yards', '20080923'
    

    (Specific documentation on Table Value Constructors in SQL Server. I can't find specific separate documentation on row value constructors, but that's what they are)

    0 讨论(0)
  • 2020-12-17 23:18

    For SQL-Server: Yes, and it can exactly like you write. Just be certain that the column values are in the same order as they appear in the table. Also: you must supply a value for each existing column.

    For Access 2010: No. At least not by hardcoded values in the sql, but only by selecting multiple records from a table (in the same or in another database). See also the link in the answer of Khepri.

    0 讨论(0)
  • 2020-12-17 23:18

    SQL Server definitely allows this: EDIT: [As of SQL Server 2008, thank you Marc_s]

    INSERT INTO [Table]
    ([COL1], [COL2])
    VALUES
    ('1@1.com', 1),
    ('2@2.com', 2)
    

    As for the Access requirement, I'm no access guru but I found this MSDN documentation that shows how to do multiple inserts at once.

    INSERT INTO target [(field1[, field2[, …]])] [IN externaldatabase]
         SELECT [source.]field1[, field2[, …] FROM tableexpression
    

    Doing some cursory reading beyond this, you can use a "dummy" from table if all of your values are known ahead of time as in your example.

    0 讨论(0)
  • 2020-12-17 23:18

    Create a table called OneRow with a single integer column. Insert one row.

    Then:

    INSERT INTO Production.UnitMeasure
    SELECT 'FT2', 'Square Feet ', '20080923' FROM OneRow
    UNION ALL SELECT 'Y', 'Yards', '20080923' FROM OneRow
    UNION ALL SELECT 'Y3', 'Cubic Yards', '20080923' FROM OneRow
    

    Your exact syntax works on SQL Server 2008. For earlier use my above query without the FROM clauses and no helper table.

    0 讨论(0)
  • 2020-12-17 23:19

    I know I'm a bit late to the game, but I was wanting to do the exact same thing you guys mentioned in your example. I was trying to insert a new list of default rows into a table/list using Access because I've had a lot of SQL experience, I was trying to do it the same way, however as you posters have noted, it's not possible to do the Unions and such.

    However I just wanted to post a reply up here because in the case where you're manually typing in the values (string default values in this case) you can simply open Access in datasheet view, copy your data from Excel and just paste it into your Access table (or in my case, SharePoint list). You'll need to make sure you're columns are lined up exactly, but if you were going to manually type in your "insert" sql statements, just putting that info into an Excel spreadsheet shouldn't be a big deal.

    In my case, my table/list only had a single column as a lookup, so I just copied the column from notepad++ and pasted it into the datasheet view.

    Good luck everyone!

    0 讨论(0)
提交回复
热议问题