Noob question. Despite following the official PostgreSQL syntax I cannot insert multiple rows into a table. It always ends up adding all values to the first row.
I first created a table like this:
create table "DBSTest".Test (column1 varchar, column2 varchar, column3 varchar);
My update statement looks like this:
insert into "DBSTest".Test (column1, column2, column3)
values (
('test1a','test1b'),
('test2a','test2b'),
('test3a','test3b')
);
I would expect the table to look like this:
column1 | column2 | column3 |
---|---|---|
test1a | test2a | test3a |
test1b | test2b | test3b |
Instead it ends up like this:
column1 | column2 | column3 |
---|---|---|
(test1a,test1b) | (test2a,test2b) | (test3a,test3b) |
Apparently I’m getting something super wrong. I would appreciate any help on this, thanks 🙂
PS. Edited to get the table format right.
3
Answers
In the VALUES clause of INSERT, each row of data (in your sample each line of data) goes into its own row of the table, not its own column.
You are trying to insert 3 rows as (‘test1a’,’test1b’) in the first column
Try this :
SQL inserts values by order of rows, not columns, i.e. your query should look like this:
insert into dbstest (column1, column2, column3) values ('test1a', 'test2a', 'test3a'), ('test1b', 'test2b', 'test3b');
This then returns the output as you specified: