简体   繁体   中英

Syntax error when trying to insert multiple rows in SQL?

I am trying to store multiple values into a table with two columns in a single command. Here is my command,

INSERT INTO CATEGORY
VALUES('BUS','BUSINESS'), ('CHN', 'CHILDREN'), ('COK', 'COOKING'), ('COM', 
'COMPUTER'), ('FAL', 'FAMILY LIFE'), ('FIT', 'FITNESS'), ('SEH', 'SELF HELP'), 
('LIT', 'LITERATURE');

I get a red underline after my first pair of values where I have the comma. What am I doing wrong?

I would start by listing columns:

INSERT INTO CATEGORY (<colname1>, <colname2)
    VALUES ('BUS','BUSINESS'), ('CHN', 'CHILDREN'), ('COK', 'COOKING'), 
           ('COM', 'COMPUTER'), ('FAL', 'FAMILY LIFE'), ('FIT', 'FITNESS'), 
           ('SEH', 'SELF HELP'), ('LIT', 'LITERATURE');

Not all databases support VALUES with multiple rows. So that could be the cause of your problem.

If you are using, say, Oracle that doesn't support this, you can use UNION ALL :

INSERT INTO CATEGORY (<colname1>, <colname2)
    SELECT 'BUS', 'BUSINESS' FROM DUAL UNION ALL
    SELECT 'CHN', 'CHILDREN' FROM DUAL UNION ALL
    . . .
    SELECT 'LIT', 'LITERATURE' FROM DUAL;

place your column name explicitly if more column exist in your table

INSERT INTO CATEGORY(col1,col2)
VALUES('BUS,BUSINESS'),
('CHN', 'CHILDREN'), 
('COK', 'COOKING'),
('COM', 'COMPUTER'),
('FAL', 'FAMILY LIFE'),
('FIT', 'FITNESS'), ('SEH', 'SELF HELP'), 
('LIT', 'LITERATURE');

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