34

I'm new to SQL, (using SQL 2008 R2) and I am having trouble inserting multiple rows into a single column.

I have a table named Data and this is what I am trying

INSERT INTO Data ( Col1 ) VALUES
('Hello', 'World')

That code was taken from this question, but it, like many other examples I have found on the web uses 2 columns, I just want to use 1. What am I doing wrong?

Thanks

2
  • @BaliC...Are you wantint to insert "Hello" into one record and then "World" into another record...ord just enter "Hello World" into one record?
    – MikeTWebb
    Aug 3, 2012 at 19:16
  • @MikeTWebb The first one, "Hello" into one and "World" into another.
    – Bali C
    Aug 3, 2012 at 19:32

10 Answers 10

44

To insert into only one column, use only one piece of data:

INSERT INTO Data ( Col1 ) VALUES
('Hello World');

Alternatively, to insert multiple records, separate the inserts:

INSERT INTO Data ( Col1 ) VALUES
('Hello'),
('World');
43

to insert values for a particular column with other columns remain same:-

INSERT INTO `table_name`(col1,col2,col3)
   VALUES (1,'val1',0),(1,'val2',0),(1,'val3',0)
2
  • here...in a single query,col2 has added 3 different values as 3 different records with same values for col1 and col3 in all records Oct 31, 2012 at 14:15
  • 2
    +1 for covering how to add multiple specific columns to multiple rows
    – sam-w
    Oct 31, 2012 at 14:35
14

I believe this should work for inserting multiple rows:

INSERT INTO Data ( Col1 ) VALUES
('Hello'), ('World'),...
11

Another way to do this is with union:

INSERT INTO Data ( Col1 ) 
select 'hello'
union 
select 'world'
1
  • 1
    I'm not aware of any reason to use this method over the other answers below. I'd like to hear some rationale as to why this is the accepted answer. Mar 6, 2019 at 17:29
8

If your DBMS supports the notation, you need a separate set of parentheses for each row:

INSERT INTO Data(Col1) VALUES ('Hello'), ('World');

The cross-referenced question shows examples for inserting into two columns.

Alternatively, every SQL DBMS supports the notation using separate statements, one for each row to be inserted:

INSERT INTO Data (Col1) VALUES ('Hello');
INSERT INTO Data (Col1) VALUES ('World');
1
  INSERT INTO Data ( Col1 ) VALUES ('Hello'), ('World')
1

You can also follow this method

UPDATE TABLE_NAME
SET column_name = value
WHERE id IN (id_numbers);

Example:

UPDATE report_card
SET result = true
WHERE id IN (1, 3, 5, 8, 9);
0

In that code you are inserting two column value. You can try this

   INSERT INTO Data ( Col1 ) VALUES ('Hello'),
   INSERT INTO Data ( Col1 ) VALUES ('World')
0

Kindly ensure, the other columns are not constrained to accept Not null values, hence while creating columns in table just ignore "Not Null" syntax. eg

Create Table Table_Name(
            col1 DataType,
            col2 DataType);

You can then insert multiple row values in any of the columns you want to. For instance:

Insert Into TableName(columnname)
values
      (x),
      (y),
      (z);

and so on…

Hope this helps.

-3
INSERT INTO hr.employees (location_id) VALUE (1000) WHERE first_name LIKE '%D%';

let me know if there is any problem in this statement.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.