?
Solved

Microsoft Access SQL Select Into Statement

Posted on 2012-03-27
6
Medium Priority
?
516 Views
Last Modified: 2012-03-31
I have 2 tables, and use 1 of the tables to copy into the 2nd table.  Both tables have the same Primary Keys.  I use a Select into Statement: SELECT * INTO <Target> FROM <Source>

The SQL query works just fine, but, the primary key on the target table gets wiped out.  How can I use the SELECT INTO sql and not wipe out the primary key?
0
Comment
Question by:vfinato
6 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 37773245
Are you looking for an Append query (just adds rows to an existing table)?

INSERT INTO TableB (pk, fld1, fld...) SELECT (pk, fld1,fld2...  FROM TableA)

In order to copy the PK from tableA to tableB, the corresponding field in tableB should Not be an autonumber.
0
 
LVL 49

Expert Comment

by:Dale Fye
ID: 37773247
You would have to change the datatype of the primary key in the target table from Autonumber to Long Integer.
0
 
LVL 8

Expert Comment

by:gpizzuto
ID: 37773410
You can also ignore the copy of the primary key
(if it is only a counter and have another natural-key, for example some fields together):

INSERT INTO TableB (fld1, fld...) SELECT (fld1,fld2...  FROM TableA)
0
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 

Author Comment

by:vfinato
ID: 37774120
The Primary Key Field is not a "Autonumber", the field type is text, and the primary key still gets wiped out.

Also, if I don't include the Primary Key Field on the SQL query, then, that field does not get included in the <target> table.
0
 
LVL 61

Accepted Solution

by:
mbizup earned 2000 total points
ID: 37774191
If you use an insert query like in my first post, nothing gets wiped out.
0
 

Author Closing Comment

by:vfinato
ID: 37791525
This worked, thanks
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Suggested Courses
Course of the Month16 days, 13 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question