Is there any way how to create temporary table/view/stored procedure in MS Access database (2010) using Query Designer?
Whenever i try to execute something like this:
SELECT * INTO #temp_table
FROM (SELECT column_1, column_2 FROM table)
MS Access throws an error:
Syntax error in CREATE TABLE statement.
With no further information.
This will work if you add:
Access SQL does not support executing more than one SQL statement at a time, so the notion of a #temp table does not really apply. If you require a temporary table then you need to
While this has been answered for a year, I hope the following approach might be helpful to others who are, like me, trying to compensate for MS Access shortcomings in a pragmatic way and want to focus on the actual sql-queries.
It covers:
running easy to read sql-procedures stored in textfiles (the common solution of coding them in VBA with concatenated strings becomes unhandy and difficult rather soon)
using "temporary" tables. Note that this solution does not prevent accdbs from "bloating up" - so they still have to be compressed from time to time. This can be prevented if you chose to run your procedure in a temporary database, which requires some additional coding and error-handling.
(I use an empty accdb with the following code as templates and then work from there, e.g. if additional VBA-Routines or Forms are required.)
Steps:
1 - Store your SQL-statement or whole procedures in a txtfile. In an attempt to keep the VBA-code short it requires
sql statements to be closed with a semicolon ; followed by a linebreak
commented lines to span the whole line, meaning no additional inline characters before the /* and or after the */
The OP obv. has access to an sql-editor, others might not. But: even in a company environment you should usually be able to get your hands on some freeware texteditor with SQL-highlighting (like notepad++ which highlights syntax in files with .sql-extensions).
2 - Calling the routine
3 - Closing + deleting the "temporary" Tables (in my case labeled with a prefix "tmp", bc Access has issues with the # character in table names - namely: tablename has to be put in [brackets] in the sql, which results in issues when deleting those tables in a loop).
Wrapping it in "resume next" is everything but elegant, but in my case it proved the most robust solution (the goal being the coding of the actual SQL-Code, not some UI around it.)
4 - fetching the SQL-Procedure and processing the single statements in a loop.
5 - A standard function for returning the contents of a textfile as string. In this case, i also skip commented lines, which is redundant if you chose not to comment your sql.
Try this
Note: you should have an existing table from which you want to create your temporary table.
Use a SELECT statement to check your results:
As far as I know there is no way to execute a SQL statement like the one in your question in the Access Query Designer, as it expects every statement to be a View/Procedure or Function definition.
There is a general problem with you statement. When using a subselect/derived table within your FROM clause, you will have to use an alias for the returned results. This will work in SQL Server Management Studio:
I recommend using SQL Server Management Studio (or a similar tool) for most of the SQL work when developing Access Data Project (ADP) applications.