tags:

views:

32848

answers:

13

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?

+1  A: 

Remove VALUES from your SQL.

Forgotten Semicolon
+20  A: 

No "VALUES", no parenthesis:

INSERT INTO Table2(LongIntColumn2, CurrencyColumn2)
SELECT LongIntColumn1, Avg(CurrencyColumn) as CurrencyColumn1 FROM Table1 GROUP BY LongIntColumn1;
Nouveau
+1  A: 

Remove both VALUES and the parenthesis.

INSERT INTO Table2 (LongIntColumn2, CurrencyColumn2)
SELECT LongIntColumn1, Avg(CurrencyColumn) FROM Table1 GROUP BY LongIntColumn1
GSerg
+2  A: 

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

smink
A: 

Well I think the best way would be (will be?) to define 2 recordsets and use them as an intermediate between the 2 tables.

  1. Open both recordsets
  2. Extract the data from the first table (SELECT blablabla)
  3. Update 2nd recordset with data available in the first recordset (either by adding new records or updating existing records
  4. 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 ...)

Philippe Grondier
+1  A: 

I believe you 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[, …])

Sean
A: 

do you want to insert extraction in existing table?

if it does not matter they you can try the following 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

A: 

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;

Chris OC
A: 

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;User ID=sa;Password=gchaturthi').dbIPFMCI.dbo.DocTypeGroup

A: 

Thanx a lot.....It really Helped

A: 

it works fine with me .. thanks alot

Hawy.PHP
A: 

insert into t2 Select x,y from Opendatasource( 'SQLOLEDB','Data Source=nishanthalap;User ID=sa;Password=pw').db2.dbo.t1

this is work on SQL Query analyzer but not working in sp How to runn with stored procedure?

Mahawatta
This is not an answer to this question, but a new question in its own right. If it's related to this question, then create a new question of your own and refer back to this question (and explaining why the answers here didn't do the job for you).
David-W-Fenton
A: 

I Have the table1 in that id,name,sex are the field. in my second table deptid,deptname,deptloc.now my question is how to insert the dept location values into table1? actually i dont have the column on the name of dept location.it is new to create.

venkat
Is this a question? If so, it doesn't belong here -- you should create a new question of your own. If it's an answer, I don't understand how it applies.
David-W-Fenton