I'm trying to write a query that extracts and transforms data from a table and then insert those data into another table. Yes, this is a data warehousing query and I'm doing it in MS Access. So basically I want some query like this:
INSERT INTO Table2(LongIntColumn2, CurrencyColumn2) VALUES
(SELECT LongIntColumn1, Avg(CurrencyColumn) as CurrencyColumn1 FROM Table1 GROUP BY LongIntColumn1);
I tried but get a syntax error message.
What would you do if you want to do this?
No "VALUES", no parenthesis:
INSERT INTO Table2(LongIntColumn2, CurrencyColumn2)
SELECT LongIntColumn1, Avg(CurrencyColumn) as CurrencyColumn1 FROM Table1 GROUP BY LongIntColumn1;
You have two syntax options:
Option 1
CREATE TABLE Table1 (
id int identity(1, 1) not null,
LongIntColumn1 int,
CurrencyColumn money
)
CREATE TABLE Table2 (
id int identity(1, 1) not null,
LongIntColumn2 int,
CurrencyColumn2 money
)
INSERT INTO Table1 VALUES(12, 12.00)
INSERT INTO Table1 VALUES(11, 13.00)
INSERT INTO Table2
SELECT LongIntColumn1, Avg(CurrencyColumn) as CurrencyColumn1 FROM Table1 GROUP BY LongIntColumn1
Option 2
CREATE TABLE Table1 (
id int identity(1, 1) not null,
LongIntColumn1 int,
CurrencyColumn money
)
INSERT INTO Table1 VALUES(12, 12.00)
INSERT INTO Table1 VALUES(11, 13.00)
SELECT LongIntColumn1, Avg(CurrencyColumn) as CurrencyColumn1
INTO Table2
FROM Table1
GROUP BY LongIntColumn1
Bear in mind that Option 2 will create a table with only the columns on the projection (those on the SELECT).
Remove both VALUES and the parenthesis.
INSERT INTO Table2 (LongIntColumn2, CurrencyColumn2)
SELECT LongIntColumn1, Avg(CurrencyColumn) FROM Table1 GROUP BY LongIntColumn1
I believe your problem in this instance is the "values" keyword. You use the "values" keyword when you are inserting only one row of data. For inserting the results of a select, you don't need it.
Also, you really don't need the parentheses around the select statement.
From msdn:
Multiple-record append query:
INSERT INTO target [(field1[, field2[, …]])] [IN externaldatabase]
SELECT [source.]field1[, field2[, …]
FROM tableexpression
Single-record append query:
INSERT INTO target [(field1[, field2[, …]])]
VALUES (value1[, value2[, …])
Remove VALUES from your SQL.
Remove "values" when you're appending a group of rows, and remove the extra parentheses. You can avoid the circular reference by using an alias for avg(CurrencyColumn) (as you did in your example) or by not using an alias at all.
If the column names are the same in both tables, your query would be like this:
INSERT INTO Table2 (LongIntColumn, Junk)
SELECT LongIntColumn, avg(CurrencyColumn) as CurrencyColumn1
FROM Table1
GROUP BY LongIntColumn;
And it would work without an alias:
INSERT INTO Table2 (LongIntColumn, Junk)
SELECT LongIntColumn, avg(CurrencyColumn)
FROM Table1
GROUP BY LongIntColumn;
Well I think the best way would be (will be?) to define 2 recordsets and use them as an intermediate between the 2 tables.
Open both recordsets
Extract the data from the first table (SELECT blablabla)
Update 2nd recordset with data available in the first recordset (either by adding new records or updating existing records
Close both recordsets
This method is particularly interesting if you plan to update tables from different databases (ie each recordset can have its own connection ...)
inserting data form one table to another table in different DATABASE
insert into DocTypeGroup
Select DocGrp_Id,DocGrp_SubId,DocGrp_GroupName,DocGrp_PM,DocGrp_DocType
from Opendatasource( 'SQLOLEDB','Data Source=10.132.20.19;UserID=sa;Password=gchaturthi').dbIPFMCI.dbo.DocTypeGroup
Do you want to insert extraction in an existing table?
If it does not matter then you can try the below query:
SELECT LongIntColumn1, Avg(CurrencyColumn) as CurrencyColumn1 INTO T1 FROM Table1
GROUP BY LongIntColumn1);
It will create a new table -> T1 with the extracted information
I have 2 SQL tables, table1 and table2.
table1 has two columns and I want to insert values to these columns.
one of the columns should get a static value and the other column should get a value that is a result of a query from table2.
If I wanted to insert the static data separately I would do:
INSERT INTO table1(login_id)
VALUES ('1234');
and if I wanted to insert the dynamic value separately I would do:
INSERT INTO table1(user_uuid)
SELECT users_uuid FROM table2 where first_name like 'ortal';
How can insert both values to table1 in one action?
If I try the first query I get:
11:20:45 INSERT INTO table1(login_id ,user_uuid) VALUES ('1234') Error Code: 1136. Column count doesn't match value count at row 1 0.000 sec
INSERT INTO `users`.`table1` (`login_id`) VALUES ('1234');
ERROR 1364: 1364: Field 'user_uuid' doesn't have a default value
You can add the constants to your select list and treat them a columns:
INSERT INTO table1(user_uuid, login_id)
SELECT users_uuid, '1234' FROM table2 WHERE first_name LIKE 'ortal';
I have s MySQL database and I need to insert some specific data in a table. The data should be as follows:
SELECT id FROM a_table WHERE ... returns me a list of ids.
I need to insert n rows in second_table where n is the count of the returned rows from the first query. The second table requires 2 fields - The first one will be a record from the first query and the second one will be an integer, that I will pass from my script.
For example: If the first query returns (12,14,17,18) and the integer from my script is 5 I need to create a query, that will insert (12,5),(14,5),(17,5),(18,5) and I need this done in the database layer - I don't want to create a select statement, then create a query and then run it.
I need something like this (this is not a real query - It just shows what I need):
INSERT INTO second_table (user_id,group_id) VALUES ((12,14,17,18),5)
or to be more precise like this:
INSERT INTO second_table (user_id,group_id) VALUES ((SELECT id FROM a_table WHERE ...),5)
Is there a way to do this in SQL only (no tsql - sql only)
You can include a literal value in a SELECT:
INSERT INTO second_table (user_id, group_id)
SELECT id, 5
FROM a_table
WHERE ...
INSERT INTO
second_table
(
user_id
,group_id
)
SELECT
id
,5
FROM
first_table
WHERE
...
see the MySQL docs for more details on INSERT...SELECT syntax:
http://dev.mysql.com/doc/refman/5.1/en/insert-select.html
Hi you can try query given below
Insert into items select item_sold_qty , 5 from sales
INSERT INTO second_table
SELECT id , 5 FROM a_table WHERE ...
thanks
Hi I would like to copy entire contents from column Item under table IName to column Name under table Item both belonging to the same database.
I am giving the following query but it throws the error saying that the subquery returned more than one records. (There are around 600 records)
Insert into Item set name = (Select Item from IName)
Thanks
INSERT INTO Item (Name)
SELECT Item
FROM IName
When you want to insert into a single-column* table, INSERT works either with:
INSERT INTO table (column)
VALUES (value1),(value2), ... (valueN) ;
or with:
INSERT INTO table (column)
SELECT a_column
FROM a_table
--- optional (multiple) JOINs
--- and WHERE
--- and GROUP BY
--- any complex SELECT query
(OK, the above can work with a multiple-column table, too, as long as all the other - not explicitely stated in the INSERT statement - columns have been defined with a DEFAULT value or with AUTO_INCREMENT.)
The INSERT ... SET syntax is valid in MySQL only and can be used only when you want to insert one row exactly:
INSERT INTO table
SET column = value1 ;
is equivalent to:
INSERT INTO table (column)
VALUES (value1) ;
INSERT INTO Item (name)
SELECT Item FROM IName
Link
INSERT INTO table_one (column1) SELECT column2 FROM table_two
See MySQL Ref
I want to be able to insert into one table but only if a condition is met on another table.
for example something like this
INSERT INTO table_1 (table_2_id,y,z) VALUES (123,2,3)
WHERE (
(SELECT COUNT(table_2_id) FROM table_2 WHERE valid=1 and id=123)=1
)
Basically - I want to only insert into table_1 if the related record in table_2 is valid else it should fail.
is there a way to do this in one query or will I have to use some php+seperate query to do the check instead?
See whether this works for you:
INSERT INTO table_1 (table_2_id,y,z)
SELECT '123','2','3'
FROM table_2 WHERE valid=1 and id=123