简体   繁体   中英

Filling up an ms-access table with sql

I am very new to databases and I'm currently working with Microsoft Access 2013. The situation is that I have a huge amount of data which I wanna fill in in an already created table (Inventory) by using an SQL-statement in a query.

What I have is the following:

INSERT INTO Inventory (Col 1, Col 2, Col 3, Col 4)

VALUES ("Val 1", "Val 2", "Val 3", "Val 4"), 
("Val 5", "Val 6", "Val 7", "Val 8"),
....
("Val 9", "Val 10", "Val 11", "Val 12");

And what I want is simply this table:

Col 1  |  Col 2  |  Col 3  |  Col 4
       |         |         |
Val 1  |  Val 2  |  Val 3  |  Val 4
Val 5  |  Val 6  |  Val 7  |  Val 8
Val 9  |  Val 10 |  Val 11 |  Val 12

The problem is, that I keep getting the error Missing semicolon at the end of sql-statement . Therefore I suppose that I should add a semicolon after each line. If I do this tho, I get the error that access found characters after the semicolon.

What is the right syntax to achieve my multiple-lined INSERT INTO-Statement?

I think MS Access only allows you to insert one record at a time using INSERT . . . VALUES INSERT . . . VALUES INSERT . . . VALUES :

INSERT INTO Inventory (Col 1, Col 2, Col 3, Col 4)
    VALUES ("Val 1", "Val 2", "Val 3", "Val 4");

INSERT INTO Inventory (Col 1, Col 2, Col 3, Col 4)
    VALUES ("Val 5", "Val 6", "Val 7", "Val 8");

....

INSERT INTO Inventory (Col 1, Col 2, Col 3, Col 4)
    VALUES ("Val 9", "Val 10", "Val 11", "Val 12");

You can bulk insert using INSERT INTO ... SELECT and a union query:

INSERT INTO Inventory (Col 1, Col 2, Col 3, Col 4)
SELECT "Val 1", "Val 2", "Val 3", "Val 4"
FROM (SELECT First(ID) FROM MSysObjects) dummy
UNION ALL
SELECT "Val 5", "Val 6", "Val 7", "Val 8"
FROM (SELECT First(ID) FROM MSysObjects) dummy
UNION ALL
SELECT "Val 9", "Val 10", "Val 11", "Val 12"
FROM (SELECT First(ID) FROM MSysObjects) dummy

However, the overhead might not make this construct worthwhile, and Access does have a maximum length on single queries of ~65K characters.

To serialize and deserialize tables, I recommend using ADO and persistence . This can properly store field properties, serializing to different file formats or database formats will cause information loss.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM