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)
Related
Does Big Query support operations like "REPLACE INSERT" or something related to that?
If I run a query like this twice:
INSERT INTO table(column1) VALUES(1)
It'll create a duplicated row, is it possible to insert a row only if a column with the same value does not exist?
Thanks!
Below should make it
#standardSQL
INSERT INTO yourTable(column1)
SELECT value FROM (SELECT 1 AS value)
LEFT JOIN yourTable
ON column1 = value
WHERE column1 IS NULL
Does this work for you?
INSERT INTO table(column1)
WITH s AS (SELECT 1 src)
SELECT src FROM s WHERE NOT EXISTS (
SELECT * FROM table t WHERE t.column1 = s.src
)
I need to select a few columns from a table1. I need to insert only one of these columns as well as some arbitrary hard coded data and insert it into table2 while also getting the original select statement back.
Basically I would like to get the results of my INSERT INTO statement instead of the "(1 row(s) affected)" that I get in SSMS.
Is there a way to do this?
Here is a SQLFiddle:
http://sqlfiddle.com/#!3/e9beb/3
Those records will insert just fine. However, I want the results of my SELECT statement to come back to me so that I can do it all at once without multiple reads or trips. Is this possible?
You can use the OUTPUT clause:
INSERT INTO Table2
OUTPUT inserted.*
SELECT Phrase, 'This is an automatic note by the system', GETDATE(), 1
FROM Table1
Use a batch statement and store the intermediate results in a table variable:
DECLARE #intermediate TABLE (
col1 type,
col2 type,
col3 type
);
INSERT INTO #intermediate VALUES ( ... );
INSERT INTO destinationTable SELECT * FROM #intermediate;
SELECT #intermediate;
If using this from code you can have all of this in a single command-text string.
Have you tried something like this:
INSERT INTO Table1 (
SELECT Phrase, Notes, GetDate, 1
FROM Table2
UNION
SELECT Phrase, 'This is an automatic note by the system', GETDATE(), 1
UNION
)
So for each distinct value in a column of one table I want to insert that unique value into a row of another table.
list = select distinct(id) from table0
for distinct_id in list
insert into table1 (id) values (distinct_id)
end
Any ideas as to how to go about this?
Whenever you think about doing something in a loop, step back, and think again. SQL is optimized to work with sets. You can do this using a set-based query without the need to loop:
INSERT dbo.table1(id) SELECT DISTINCT id FROM dbo.table0;
There are some edge cases where looping can make more sense, but as SQL Server matures and more functionality is added, those edge cases get narrower and narrower...
insert into table1 (id)
select distinct id from table0
The following statement works with me.
insert into table1(col1, col2) select distinct on (col1) col1 col2 from table0
The below query will also check the existing data in the Table2.
INSERT INTO Table2(Id) SELECT DISTINCT Id FROM Table1 WHERE Id NOT IN(SELECT Id FROM Table2);
Other Simple way to copy distinct data with multiple columns from one table to other
Insert into TBL2
Select * from (Select COL1, ROW_NUMBER() over(PARTITION BY COL1 Order By COL1) AS COL2 From TBL1)T
where T.COL2 = 1
I want to insert some values to a table (item1) from a table (item2) and date which is not in that item2 table.
How do I write a SQL insert into statement using the select statement to achieve that?
eg :
INSERT into item1(date, name, qty)
values(datevalue, select col1, col2 from item2);
This isn't working. What should I do to fix it?
insert into daily_stock(date, product_id, name, weight, qty, free_issues, sales_price,
purchased_price, category, last_purchased_date)
select
**'"+today+"'**,
productId, name, weight, newQty, freeIssues, NewSalesPrice,
NewPurchasePrice, category, datePurchased
from product
I'm using java and today is a string variable , still data is not inserting , whats wrong?
you're almost there: just use the SELECT variation for INSERT instread of VALUES, and include your data there as a constant:
insert into item1(date,name,qty)
select <datevalue>, col1, col2 from item2;
If your date comes from another table, you can do this:
insert into item1(date,name,qty)
select d.dcol1, i2.col1, i2.col2
from item2 i2 inner join table_containing_date_col d
on <some join criteria>
EDIT: you have to ensure that the data types match i.e. your has to be parsable to a date if you are savign it to a date field (which you are, hopefully!). You don't give details of your database but it would be something like this for SQL Server
cast('your date in yyyymmdd format' as datetime)
(yyyymmdd always works as it is recognisable ISO format)
or better still CONVERT
For MySql you have STR_TO_DATE, for Oracle TO_DATE etc.etc.
use INSERT INTO...SELECT statement
INSERT INTO item1 (date, name, qty)
SELECT 'value here' as datevalue, col1, col2
FROM item2
In the absence of a specific SQL server/type you are using, in T-SQL this can be done like so:
INSERT INTO Table1
SELECT '29/01/2012', T2.Column1, T2.Column2 FROM Table2 T2
You are close.
INSERT into ITEM1 (date, name, qty)
values (datevalue, (SELECT col1, col2 FROM item2));
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)