简体   繁体   中英

Insert multiple rows into single column

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

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)

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');

I believe this should work for inserting multiple rows:

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

Another way to do this is with union:

INSERT INTO Data ( Col1 ) 
select 'hello'
union 
select 'world'

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');
  INSERT INTO Data ( Col1 ) VALUES ('Hello'), ('World')

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')

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.

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

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

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