Insert multiple rows into single column - sql

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

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)

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.

Related

SQL single Quote(test'S)using sql

how to insert single column using sql insert query
insert into table value(test's);
By specifying the column name
Let's assume the table has 3 columns c1, c2, and c2
INSERT INTO myTable (c1)
VALUES ('c1v1'),
('c1v2')
OR
INSERT INTO myTable (c1)
SELECT 'c1v1' UNION
SELECT 'c1v2'
Assuming you are wanting to INSERT a value with an apostrophe, you need to use an escape:
INSERT INTO table
VALUES
('test''s')
are you asking about inserting a column? if yes then
ALTER TABLE table_name
ADD column_name datatype;
else read this article about inserting into columns, it will help. it is very detailed

How to insert some values in a SQL server table without select statement (e.g from my own created varibales)

I couldn't find any way to insert some new values for some columns in a table in SQL Server without a select statement.
For example I might have some values like #var1 and #var2 and I want to insert them in a new row in the table on the columns COL4 and COL5 and leave the other columns of the new row Null.
The following code is absolutely wrong but I wish I could write something like this:
Insert into myTable
-- add a new row under the lowest row of the table
Values
COL4=#var1
COL5=#var2
-- and leave the other columns empty
Use below query to insert data into myTable in selected columns through variables -
Insert into myTable
(COL4, COL5)
Values
(#var1, #var2)
you can use SELECT clause or VALUES clause
Insert into myTable(Col4,Col5)
SELECT #var1,#var2
Insert into myTable(Col4,Col5)
VALUES(#var1,#var2)

Splitting a postgres SQL insert statement into 2 tables using triggers

Is it possible to use postgres triggers to split an INSERT statement into 2 tables? So if you do insert into testtable (col1, col2) values (val1a, val1b), (val2a, val2b), can it be translated with triggers to something like
insert into testtable (col1) values (val1a), (val1b)
insert into anothertable (col2) values (val2a), (val2b)
Basically is it possible for testtable to not have a col2 even though the original SQL INSERT looks like col2 should exist on testtable?
How can this be accomplished using triggers?
You can have a VIEW with either triggers or rules to redirect the INSERT.
INSERT ...RETURNING .. comes up empty when BEFORE trigger cancels statement
Or you can do it in a single SQL statement with data-modifying CTEs.
WITH input(col1, col2) AS (
VALUES
(text 'val1a', text 'val1b') -- explicit type cast in first row
, ('val2a', 'val2b')
)
, ins1 AS (
INSERT INTO testtable (col1)
SELECT col1 FROM input
)
INSERT INTO anothertable (col2)
SELECT col2 FROM input;
Typically, one would also store the connections between 'val1a' and 'val1b' of your input row somehow.
You might want to use a RETURNING clause to get a serial PK from the first table and store that in the second table.
Related:
PostgreSQL multi INSERT...RETURNING with multiple columns
Combining INSERT statements in a data-modifying CTE with a CASE expression
INSERT rows into multiple tables in a single query, selecting from an involved table

SQLite : insert multiple value (select from) with insert specified value

i'm trying to make sqlite query that can insert multiple values , here's my query that i try :
insert into table1(idy_table1,idx_table1)
values ('1', //specified value insert to idy_table1
(select id_table2 from table2)) //insert value from select id_table2
i'm having some trouble, it just only insert one value,
and my question is how to make a proper query? so i can make it work.
The VALUES clause always adds one row.
(Except when you're using multiple tuples, but this does not work with queries.)
The easiest way to add multiple rows from a query is to use the SELECT form of the INSERT statement:
INSERT INTO Table1(idy_table1, idx_table1)
SELECT '1', id_table2 FROM table2;

sql insert only two values into a table with 10 fields

My table has 10 fields: field1, field2,..., field10
Now suppose i only want to insert values into first two columns, and put '' on the rest.
I'm currently using
insert into table1 values
(100,200,'','','','','','','','','','')
I wonder if there is a better way to avoid ,'','','','' ?
Thanks for advice!
Yes, just specify the fields you want to add like this:
insert into table1 (field1, field2) VALUES (100,200)
In this case field1 will insert 100, and field2 will insert 200.
The other fields in your table will be null, so you need to make sure that is allowed.
EDIT:
in the comments, #andreas is correct - any field you don't specify will be given it's default value (which may be null)