Link to home
Start Free TrialLog in
Avatar of camper12
camper12

asked on

create a temp table

I have a huge table with 20 columns. I only want to use 3 columns to create a temp table and then insert results into it.

I am pretty new to all this. The data structures have to match for insertion. So, how do I find the data types and then use specific columns to create table and then insert values into the table.

Thanks
Avatar of Robert Sherman
Robert Sherman
Flag of United States of America image

You can do this with a "Make Table" query.   Create a new query in design mode.   Add the table you want the data to come from.   Change its type to "Make Table".  It will ask you for a name for the new table.   Enter it and select OK.  Then just add the fields you want to the query.  

When you run it, it will put the results into the new table.
SOLUTION
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>I only want to use 3 columns to create a temp table and then insert results into it.
Another possibility is to create a view that only uses the one table / three columns, and then insert into the view.

This is especially good if it's desired to only allow certain users/roles to perform this action, privs to the view can be granted to only those users/roles.

This is dependant on the other 17ish columns allowing NULL values, otherwise any attempt to insert only those three columns will fail.
It's becoming clear from the answers so far that more information on what you are trying to accomplish might be helpful.

First, why are you creating this temporary table?    Do you really need to create a temporary table (and duplicate data) or can you use a query or view to limit what is accessible?  Further, since views have been brought up, and your question is tagged with MS Access AND SQL Server, exactly which of the those two is your data stored in.    

I think Jim's answer leans towards SQL Server, and is the way to go if your goal is just to limit what fields can be accessed  by certain users.. however, since that solution is inserting into the original table, it brings up my earlier questions about the temporary table.

EDIT: Topic tags also include MySQL, so the plot thickens...
Yes, why add records to a temp table?

Please describe your current configuration and your ultimate goal.

When you frame the question in terms of what you want to do, (create temp tables and add records)
...then it prohibits us from suggesting a more efficient technique.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also, you can use something like this. In this example from Table1 you want to create a table (in database or temp table). Table1 has many columns but the new table needs to have only column f1, f2 and f3:
IF OBJECT_ID('MyNewTable') IS NOT NULL DROP TABLE MyNewTable;
GO
WITH cte AS
(
SELECT
f1
, f2
, f3
FROM Table1
)
SELECT *
INTO MyNewTable
FROM cte;

Open in new window


Mike
Note to self:  Add to article Top 10 Ways to Ask Better Questions 'I've answered once, so did five other people, and we're waiting for a response before commenting again'
LOL @Jim.   I was thinking "..must be a slow news day."