简体   繁体   中英

Insert into table without having to specify every column value for each row

How do I insert into a table where the number of column value supplied is different for each row. If there is no value for a column.

CREATE TABLE myTable (column1 VARCHAR(5), column2 VARCHAR(5), column3 VARCHAR(5),
column4 VARCHAR(5), column5 VARCHAR(5), column6 VARCHAR(5))
INSERT INTO myTable ('A','B','C','D'),('A','B','C'),('A','B'),('A'),
('B','C','D'),('C','D','A')

I am fully aware of having the each row inserted by using NULL for the column that is empty.

CREATE TABLE myTable (column1, column2, column3, column4, column5, column6)
INSERT INTO myTable ('A','B','C','D',NULL,NULL),('A','B','C',NULL,NULL,NULL),
('A','B',NULL,NULL,NULL,NULL),('A',NULL,NULL,NULL,NULL,NULL),
('B','C','D',NULL,NULL,NULL),('C','D','A',NULL,NULL,NULL)

Is there a way to insert each row without having to specifying the column as NULL if there column value is not supplied?

You can insert them separately:

insert into mytable (col1, col2, col3, col4) values ('A', 'B', 'C', 'D');
insert into mytable (col1, col2, col3) values ('A', 'B', 'C'), ('D', 'E', 'F');
insert into mytable (col3, col4) values ('C', 'D');

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