Insert Into Statement - Omitting one Column - sql

I am trying to insert into a table all data but change just the date column
So far I have something like
INSERT INTO MyTable (SELECT * FROM MyTable...)
What I want to do is set one of the columns to be a constant value (like todays date instead of what is selected out)
Does anyone know of an easy way of doing that

INSERT INTO MyTable (col1, col2, col3) (SELECT col1, col2, 'constant' FROM MyTable...)

Yes, you can do it like so:
INSERT INTO MyTable (SELECT MyTable.Id, MyTable.xxx, CONSTANT_VALUE FROM MyTable ...)

I would simply do the ffg
INSERT INTO MyTable
Select col1, col2,....., THE_CONSTANT_VALUE, SOME_OTHER_VALUE from MyTable

You can do this if you specify the columns you are inserting into, something like this:
INSERT INTO MyTable (Col1, Col2, DateVal, StaticValue)
(SELECT Col1, Col2, getdate() As DateVal, 'test' As StaticValue FROM MyTable2)
This would get Col1 and Col2 from MyTable2 but use the getdate() function to get the current date for the DateVal column, and every row inserted would have 'test' for the StaticValue column.
Does this help?

To update all the date field of every row in MyTable to the current date do:
UPDATE MyTable SET thedatecolumn = GETDATE()
If you want to insert from Table1 into Table2 where they have the same structure do:
INSERT INTO Table2 (column1, column2, column3, fixedcolumn) (SELECT column1, column2, 'textvalue' AS staticcolumn, GETDATE() AS functioncolumn FROM Table1)

Related

PGSQL - How to use output of a select statement in another query?

I realized I didn;t do a good job in asking my question. So giving it a try again.
I am trying to create a pgsql function with below requirement.
I need to store the output of a sql statement in a row/record and use in another statement which selects from third table does some mathematical calculation and insert it second table.
exp:
row = select * from table1;
insert into table2(col21,col22,col23,col24,col25)
values(
select
(col31 - row.col11)/row.col15,
(col32 - row.col12)/row.col14,
(col33 - row.col13)/row.col13,
(col34 - row.col14)/row.col12,
(col35 - row.col15)/row.col11
from table3
);
I would like to know how can I achieve it with pgsql.
Thanks in advance.
No need for anything fancy; normal SQL has you covered:
insert into table2(col1,col2,col3,col4,col5)
select
(b.col31 - a.col11)/a.col15,
(b.col32 - a.col12)/a.col14,
(b.col33 - a.col13)/a.col13,
(b.col34 - a.col14)/a.col12,
(b.col35 - a.col15)/a.col11
from table1 a
cross join table3 b
Should be fairly standard sql.
INSERT INTO TABLE2 (col1, col2, col3, col4, col5)
VALUES (
SELECT * -- Assuming there's only 5 Columns from Table1
FROM TABLE1
)
If you're looking for some sort of temporary table to alter data later and be able to revert back;
CREATE TEMP TABLE temp_table1 AS
SELECT * from table1;
Further note:
If your first column is an automatically incremented pkey and you have data in your 2nd table already - you should just insert values for non-increment columns or you'll have other errors.
INSERT INTO TABLE2 (col2, col3, col4, col5)
VALUES (
SELECT col2, col3, col4, col5 -- Assuming there's only 5 Columns from Table1 and Col1 is a pkey.
FROM TABLE1
)
Basic SQL:
INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;
Source: https://www.w3schools.com/sql/sql_insert_into_select.asp
Thank you everyone for your answer. Below is what I came up with.
Ref : https://www.postgresql.org/docs/8.2/static/plpgsql-declarations.html
DECLARE
row table1%rowtype;
BEGIN
select * into strict row from table1;
insert into table2(col21,col22,col23,col24,col25)
values(
select
(col31 - row.col11)/row.col15,
(col32 - row.col12)/row.col14,
(col33 - row.col13)/row.col13,
(col34 - row.col14)/row.col12,
(col35 - row.col15)/row.col11
from table3
);
END;

Oracle: Insert into select... in the

What is the advantage of inserting into a select of a table over simply inserting into the table?
eg
insert into
( select COL1
, COL2
from Table1
where 1=2 <= this and above is the focus of the question.
) select COL3, COL4 from Table2 ;
It seems to do the same thing as:
insert into Table1
( COL1, COL2 )
select COL3, COL4 from Table2 ;
This is the first time I've seen this; our Sr Dev says there is some advantage but he can't remember what it is.
It may make sense in a way if one was inserting a "select *..." from a table with lots of columns, and we want to be lazy, but... we're not. We're enumerating each column in the table.
Database is Oracle 11gR2, but this query was written probably in 10g or before.
we want to be lazy
No, we use insert into table(col1, col2) select col2, col2 from ... when there is a lot of records (for example 1M) and we don't want to create a the values section for each. Let's imagine how much time it takes if you write
insert into table (col1, col2)
values (select col1, col2 from (select col1, col2, rownum rn from ...) where rn = 1);
insert into table (col1, col2)
values (select col1, col2 from (select col1, col2, rownum rn from ...) where rn = 2);
...
insert into table (col1, col2)
values (select col1, col2 from (select col1, col2, rownum rn from ...) where rn = 1000000);
insert select is faster way for copying data from one table(several tables) to an another table.
In a nutshell. It's a lot easier. Especially when you have a massive query that you dont wanna rebuild,or if you have a crapton of objects, or values you are inserting.
Without WITH CHECK OPTION specified, I don't know of any purpose for this syntax. If you specify WITH CHECK OPTION, you can effectively implement an ad-hoc check constraint within your insert statement.
insert into
( select COL1
, COL2
from Table1
where 1=2 WITH CHECK OPTION
) select COL3, COL4 from Table2 ;
The above will never insert a record, because 1 will never equal 2.
The statement below will insert a record as long as COL3 is less than 100, otherwise an exception is raised.
insert into
( select COL1
, COL2
from Table1
where COL1 < 100 WITH CHECK OPTION
) select COL3, COL4 from Table2 ;

Copy data into another table

How to copy/append data from one table into another table with same schema in SQL Server?
Edit:
let's say there is a query
select *
into table1
from table2
where 1=1
which creates table1 with the same schema as well as data as in table2.
Is there any short query like this to only copy entire data only into an already existing table?
If both tables are truly the same schema:
INSERT INTO newTable
SELECT * FROM oldTable
Otherwise, you'll have to specify the column names (the column list for newTable is optional if you are specifying a value for all columns and selecting columns in the same order as newTable's schema):
INSERT INTO newTable (col1, col2, col3)
SELECT column1, column2, column3
FROM oldTable
Simple way if new table does not exist and you want to make a copy of old table with everything then following works in SQL Server.
SELECT * INTO NewTable FROM OldTable
This is the proper way to do it:
INSERT INTO destinationTable
SELECT * FROM sourceTable
INSERT INTO table1 (col1, col2, col3)
SELECT column1, column2, column3
FROM table2
Try this:
INSERT INTO MyTable1 (Col1, Col2, Col4)
SELECT Col1, Col2, Col3 FROM MyTable2
Try this:
Insert Into table2
Select * from table1
Insert Selected column with condition
INSERT INTO where_to_insert (col_1,col_2) SELECT col1, col2 FROM from_table WHERE condition;
Copy all data from one table to another with the same column name.
INSERT INTO where_to_insert
SELECT * FROM from_table WHERE condition;
INSERT INTO DestinationTable(SupplierName, Country)
SELECT SupplierName, Country FROM SourceTable;
It is not mandatory column names to be same.
CREATE TABLE `table2` LIKE `table1`;
INSERT INTO `table2` SELECT * FROM `table1`;
the first query will create the structure from table1 to table2 and second query will put the data from table1 to table2
Copy all columns from one table to another table:
INSERT INTO table2
SELECT * FROM table1
WHERE condition;
Copy only some columns from one table into another table:
INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;
You can duplicate or "clone" a table's contents by executing:
CREATE TABLE new_table AS SELECT * FROM original_table;
-- for Sql Server users.
if you don't have the new table then you can create the new table with same structure as old table, and also copy data over from old table to the new table. For example:
select * into new_table
from old_table;
also you can copy the column / table structure, and just some of data. For example:
select * into new_table
from old_table
where country = 'DE';

How to select the record from table and insert into another table?

I wanted to select the last record from the table1 and insert into another table .Here is my query.
Insert into table2 values(select top 1 col1,col2 from table1 order by id desc).
I know for adding the value into table,need to be in cotation.But where to add?
You can select literals to fill in the other columns that table1 can't provide, something like this:
insert into table2 (col_a, col_b, col_c, col_d)
select top 1 col1, col2, 'foo', 'bar'
from table1
order by id desc
Any columns you do not name in the column list will get the default value, or null if no default is defined.
The number and type of columns selected must match the number and type of columns in the insert column list.
In SQL, there are essentially basically two ways to INSERT data into a table: One is to insert it one row at a time, the other is to insert multiple rows at a time. Let's take a look at each of them individually:
INSERT INTO table_name (column1, column2, ...)
VALUES ('value1', 'value2', ...)
The second type of INSERT INTO allows us to insert multiple rows into a table. Unlike the previous example, where we insert a single row by specifying its values for all columns, we now use a SELECT statement to specify the data that we want to insert into the table. If you are thinking whether this means that you are using information from another table, you are correct. The syntax is as follows:
INSERT INTO table1 (column1, column2, ...)
SELECT t2.column3, t2.column4, ...
FROM table2 t2
So, in you case, you can do it like this:
Insert into table2
select top 1 t1.col1,t1.col2 from table1 t1 order by id desc
Or you can use your syntax like this:
declare #col1 type_of_col1, #col2 type_of_col2
select top 1 #col1 = t1.col1, #col2 = t1.col2 from table1 t1 order by id desc
Insert into table2 values(#col1, #col2)
Offcourse, this all works assuming that the column datatypes are matched.

Is this possible with sql?

Is it possible to do something like this:
INSERT INTO table(col1, col2) VALUES(something_from_another_table, value);
With something_from_another_table being a SQL command? Like, is there something I can do that's equivelant to:
INSERT INTO table(col1, col2) VALUES((SELECT value FROM table2 WHERE id = 3), value);
Yes
INSERT INTO table(col1, col2)
SELECT value1, 'value2' FROM table2 WHERE id = 3
Where value1 is the value from the 'other table' and value2 is a constant that you've included in that select statement.
Try this:
INSERT INTO table(col1, col2)
SELECT table2.value1, value2 FROM table2 WHERE table2.id = 3;
http://dev.mysql.com/doc/refman/5.1/en/insert-select.html
take a look especially in the examples.
I would recommend reading full syntax of SELECT, UPDATE, DELETE and INSERT SQL commands to begin with. Then expand to subqueries and DDL.
Go slowly and work out examples.
You definately can. It should work similar as the example below;
INSERT INTO Store_Information (store_name, Sales, Date)
(SELECT store_name, Sales, Date FROM Sales_Information WHERE Year(Date) = 2010)
when you specify the keyword "Values" on the insert statement you are trying to insert just a value. the current way to do that is assigning the value of the "something_from_another_table" into a variable and then, make your insert
DECLARE #ANYVALUE AS VARCHAR(40)
SELECT #ANYVALUE = ANYFIELD FROM table2 WHERE id = 3
INSERT INTO table1 (FIELD1, FIELD2) VALUES(#ANYVALUE, VALUE2)
On this way always will insert one record. the other correct way will insert n record as the where statement can filter.
INSERT INTO Store_Information (store_name, Sales, Date)
(SELECT store_name, Sales, Date FROM Sales_Information WHERE Year(Date) = 2010)