Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert multiple rows into single column

Tags:

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

like image 241
Bali C Avatar asked Aug 03 '12 19:08

Bali C


People also ask

How do I insert multiple rows into one column?

Tip: To insert more than one row (or column) at the same time, select as many rows or columns as you want to add before you click the insert control. For example, to insert two rows above a row, first select two rows in your table and then click Insert Above.

How do I insert data into a single column in SQL?

INSERT INTO Syntax It is possible to write the INSERT INTO statement in two ways: 1. Specify both the column names and the values to be inserted: INSERT INTO table_name (column1, column2, column3, ...)

How do I combine multiple rows into one column in SQL?

You can concatenate rows into single string using COALESCE method. This COALESCE method can be used in SQL Server version 2008 and higher. All you have to do is, declare a varchar variable and inside the coalesce, concat the variable with comma and the column, then assign the COALESCE to the variable.


2 Answers

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) 
like image 164
user1633492 Avatar answered Oct 13 '22 11:10

user1633492


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'); 
like image 20
JYelton Avatar answered Oct 13 '22 10:10

JYelton