For loop in SQL - sql

greetings all
I want to insert 70 records in a table
the values are value1,value2,...value70
and I was wondering if it's possible to accomplish this through a SQL query
I am using postgresql db.
thanks in advance.

Where do the values come from? Are they arbtrary values? Just use a multiple insert statement, like:
INSERT INTO t (colname) VALUES ('value1'), ('value2'), ..., ('value70');
Are they values that can be generated from the integers 1 to 70? If so, use the set returning function generate_series:
INSERT INTO t (colname) SELECT 'value'||i FROM generate_series(1,70) AS s(i);
Are they just in some other table? Just reference it normally:
INSERT INTO t (colname) SELECT val FROM othert;

Related

Insert comma delimited ids into a table

I need to insert records into a SQL Server table. The client send ids I need to insert into a specific column in the table:
(2,4,123,1357,1234,5657,753);
Using this function I'm splitting the comma delimited string, but not sure how to insert it to the table along with the other columns
I need to create something that will generate inserts such as:
insert into table_name (id,column_2,column_3) values (2, column_s_some_value, column_3_some_value);
insert into table_name (id,column_2,column_3) values (4, column_s_some_other_value, column_3_some_value);
insert into table_name (id,column_2,column_3) values (123, column_s_some_value, column_3_some_value);
ETC...
How can I achieve that?
the split function is a Table-Valued Function, which means it can be treated as a table, and you can do an INSERT..SELECT
insert into table_name (id,column_2,column_3)
SELECT s.item, column_s_some_value, column_3_some_value
FROM Split(#input_string, ',') s
{JOINS if needed to get other column values}
If you do want to split strings, the most efficient way to get that done is through the use of the tally table as outlined here by Jeff Moden. I'd strongly suggest you use this method in just about any version of SQL Server.

INSERT statement for one column to populate each row

I have a table called books that I just altered to have a column called yearPUB. I'm trying to populate each row with a year but it's not working. This is the INSERT statement I'm using.
INSERT INTO books (yearPub)
VALUES (2002),
(2006),
(1999),
(2005),
(2003),
(2001),
(1998),
(1968),
(2009),
(1988),
Can someone tell me why it doesn't work?
You need to insert them one at a time, e.g.:
INSERT INTO books (yearPub) VALUES (2002);
INSERT INTO books (yearPub) VALUES (2006);
Alternatively, you could insert using a subquery with a select statement. The syntax may differ depending on what database you are using. For example, you could follow an example here and write something like:
INSERT INTO books (yearPub)
SELECT yearNumber FROM othertablename;

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;

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 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.

SQL: Insert multiple sets of values in one statement?

Is it possible to insert multiple sets of values to a SQLite table in one statement?
I was trying:
INSERT INTO the_table VALUES (1,2,'hi'),(2,0,'foo');
with the different ()s representing different insert sets, but I get an error.
Are there only three columns in your table? If not, you could try defining the column names you are setting like so:
INSERT INTO the_table
(column1 ,column2 ,column3)
VALUES (1 ,2 ,'hi' )
,(2 ,0 ,'foo' )
This convention was introduced in SQL Server 2008 known as the Table Value Constructor. See MSDN's INSERT page for a look at the overall syntax. Also, the INSERT statement can be easily formatted for better readability.
You can do
INSERT INTO the_table
SELECT 1,2,'hi'
UNION
SELECT 2,0,'foo';
I was found that syntax in MSDN but after trying I can't do that too, than I note that in the bottom of the page was written that there is an error in the page :) where is link http://msdn.microsoft.com/en-us/library/ms174335.aspx see the bottom How to insert multiple rows