0

I run this Statement:

CREATE TABLE Persons
(
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);

INSERT INTO Persons (PersonID, LastName, FirstName, Address, City)
VALUES ((001, 002), ("f", "Doe"), ("f", "John"), ("6256 german Ave S", "1234 random ave"), ("berlin", "Rondomville"))

and get this error:

Operand should contain 1 column(s)

I don't know what exactly i'm doing wrong here.

If I run the Statement below everthing works fine:

CREATE TABLE Persons
(
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);

INSERT INTO Persons (PersonID, LastName, FirstName, Address, City)
VALUES(001, "f", "a", "6254 german Ave S", "berlin")
1
  • Group the values by rows, not by columns. Commented Jan 15, 2016 at 19:28

2 Answers 2

6

The correct syntax for this is:

INSERT INTO Persons 
            (PersonID, LastName, FirstName, Address, City)
VALUES      (1, 'f', 'f', '6256 german Ave S', 'berlin'),
            (2, 'Doe', 'John', '1234 random ave', 'Rondomville')

Alternatively, you can do a INSERT INTO ... SELECT with a UNION ALL:

INSERT INTO         Persons 
                    (PersonID, LastName, FirstName, Address, City)
SELECT              1, 'f', 'f', '6256 german Ave S', 'berlin'
UNION ALL SELECT    2, 'Doe', 'John', '1234 random ave', 'Rondomville'
Sign up to request clarification or add additional context in comments.

3 Comments

Your alternate answer looks a lot like my answer. Just sayin'
It's a very common query to use. Just sayin'.
Thanks, it's always the simple errors that cause the most frustration
1
INSERT INTO Persons (PersonID, LastName, FirstName, Address, City)
Select 001, "f", "a", "6254 german Ave S", "berlin"
UNION ALL
Select 002, "f", "a", "333 deutch strasse", "freiburg"

1 Comment

The reason I prefer this over the "values" way...is that I can select just the "select" portion of the code and run it. In a stored procedure, I could temporarily comment out the "insert" part and get the select query to run. You cannot do that with the "values" syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.