views:

105

answers:

5

how to convert result of an select sql query into a new table in msaccess ?

+4  A: 

Like so:

SELECT    *
INTO      NewTable
FROM      OldTable
Maximilian Mayerl
Not sure Access will actually *replace* `NewTable` in that situation, it may actually append stuff to the table instead, which then would fully match the user's request as I understand it.
Romain
@Romain Muller: Why don't you fire up Access and find out the answer to your question, instead of muttering out loud about it?
David-W-Fenton
In point of fact, if you run that SQL in the Access QBE, it will ask you if you want to replace the existing table. If you run it with DoCmd.RunSQL with SetWarnings ON, it will also prompt you. If you attempt to execute it in DAO, it will fail because the table already exists. In no case will it append the records to an existing table.
David-W-Fenton
A: 
Select *
Into newtable
From somequery
Arvo
A: 

If you want to do it through the user interface, you can also:

A) Create and test the select query. Save it.

B) Create a make table query. When asked what tables to show, select the query tab and your saved query.

C) Tell it the name of the table you want to create.

D) Go make coffee (depending on taste and size of table)

mavnn
+3  A: 

You can use sub queries

SELECT a,b,c INTO NewTable 
FROM (SELECT a,b,c
FROM TheTable
WHERE a Is Null)
Remou
Why use a subquery? i.e. why not add the INTO into the inner query?
onedaywhen
Note the resulting 'table' will not have a key, therefore is not a table at all.
onedaywhen
A: 

First, create a table with the required keys, constraints, domain checking, references, etc. Then use an INSERT INTO..SELECT construct to populate it.

Do not be tempted by SELECT..INTO..FROM constructs. The resulting table will have no keys, therefore will not actually be a table at all. Better to start with a proper table then add the data e.g. it will be easier to trap bad data.

For an example of how things can go wrong with an SELECT..INTO clause: it can result in a column that includes the NULL value and while after the event you can change the column to NOT NULL the engine will not replace the NULLs, therefore you will end up with a NOT NULL column containing NULLs!

Also consider creating a 'viewed' table e.g. using CREATE VIEW SQL DDL rather than a base table.

onedaywhen