I want to add multiple entries, with 1 column staying the same, and 1 column increasing by 1 every time.
To do this manually I would have to do
INSERT INTO table (column1, column2) VALUES ('1'::integer, '1'::integer)returning column1, column2;
INSERT INTO table (column1, column2) VALUES ('1'::integer, '2'::integer)returning column1, column2;
INSERT INTO table (column1, column2) VALUES ('1'::integer, '3'::integer)returning column1, column2;
etc
Is there a way I could do the numbers 1 to 34000 in 1 query?
Use generate_series():
INSERT INTO table (column1, column2)
SELECT 1, gs.n
FROM generate_series(1, 34000) gs(n);
Note: There is no need to convert a string to an integer. In general, a number literal is fine. In any case, Postgres would convert the number literal to the correct type if needed (say int or numeric or float or whatever).
Related
Noob question. Despite following the official PostgreSQL syntax I cannot insert multiple rows into a table. It always ends up adding all values to the first row.
I first created a table like this:
create table "DBSTest".Test (column1 varchar, column2 varchar, column3 varchar);
My update statement looks like this:
insert into "DBSTest".Test (column1, column2, column3)
values (
('test1a','test1b'),
('test2a','test2b'),
('test3a','test3b')
);
I would expect the table to look like this:
column1
column2
column3
test1a
test2a
test3a
test1b
test2b
test3b
Instead it ends up like this:
column1
column2
column3
(test1a,test1b)
(test2a,test2b)
(test3a,test3b)
Apparently I'm getting something super wrong. I would appreciate any help on this, thanks :)
PS. Edited to get the table format right.
In the VALUES clause of INSERT, each row of data (in your sample each line of data) goes into its own row of the table, not its own column.
You are trying to insert 3 rows as ('test1a','test1b') in the first column
Try this :
insert into Test (column1, column2, column3)
values
('test1a','test2a', 'test3b'),
('test1b','test2b', 'test3b');
SQL inserts values by order of rows, not columns, i.e. your query should look like this:
insert into dbstest (column1, column2, column3) values ('test1a', 'test2a', 'test3a'), ('test1b', 'test2b', 'test3b');
This then returns the output as you specified:
I want to select data from TABLE A and insert data into TABLE B. Though I know how to achieve this. I have used:
INSERT INTO TableA(column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM TableB
WHERE condition;
But the challenge is the data type. The target TABLE A has a column with data type definition as
column1(decimal(16,3),null)
Whereas the table TABLE B has a column with data type definition as
column1(decimal(15,4),null)
Please help me here!
It should be fine. SQL Server will automatically convert the values. If you have a very large value in B that doesn't fit in A, then you could get an error.
To solve that, use try_convert():
INSERT INTO TableA(column1, column2, column3, ...)
SELECT TRY_CONVERT(decimal(15,4), column1), column2, column3, ...
FROM TableB
WHERE condition;
This will avoid a type conversion error.
I'm using SQL Server 2012 to try to take the values of one column in a table and put them into the values of another column table in another. If I try to run the following query:
INSERT INTO table2 (column3)
SELECT column3
FROM table1
WHERE (ScopeID IS NOT NULL)
ORDER BY Name
For table2, column3 is the same type (an int), NULL values are allowed. But when I try to execute the query, it returns:
Cannot insert the value NULL into column 'column1', table 'dbo.table2';, column does not allow nulls. INSERT fails.
But I'm not trying to insert into column1... Is it just a syntax thing where the order of the columns HAVE to match?
You are inserting into column1. Remember, you are inserting entire rows of values, so you should really have a value for all columns. Your query is equivalent to:
INSERT INTO table2 (column1, column2, column3)
SELECT NULL, NULL, column3
FROM table1
WHERE (ScopeID IS NOT NULL)
ORDER BY Name;
(and so on for all the columns in the table.)
I am guessing that you actually want an update, but your question doesn't provide enough information to give further guidance.
I've found ways to pass an array to a stored procedure and ways to insert a table into another table. But I want to insert my array in a table as column2 with one other value I have as the value for column1:
INSERT INTO Table1 (column1, column2)
VALUES (SELECT #value, column2 from #otherTable)
I tried inserting the array into column2 first and then updating column1 to be the one value. But that didn't work and would be insanely expensive anyway.
Is there a reasonable way to do this?
If I'm understanding you correctly, then all you need is to get rid of the VALUES, like so:
INSERT INTO Table1 (column1, column2)
SELECT #value, column2 from #otherTable;
how to insert a row back in same table with a column count increase by +1
insert into Columns
select columns
where count of column 3 increase by 1
Do you mean something like this:
insert into YourTable (column1, column2, column3, column4)
select column4, column1, column2, column3 from YourTable
In that case data will be copied into YourTable and data from first column will be in second column, from second in third ... data from the last column will be in first.
If you just want to take a column and increase the value of one column by one, I don't see why you'd take it out and insert it again when you can just UPDATE the data:
UPDATE sometable
SET somecolumn = somecolumn + 1
WHERE someothercolumn = somevalue
Based on your comment, maybe you just want an identity column.
If you create a table like this:
CREATE TABLE myTable (
id INT GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1)
)
This should make it so every time you insert a row into myTable, the new row has an id which is one greater than any other in the table.
Presumably, you can ALTER a table to add an identity column too.
I used DB2 syntax because that's what this is tagged as. If you're using another database, the syntax will be much simpler.