Solved

Microsoft Access SQL Select Into Statement

Posted on 2012-03-27
6
443 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 47

Expert Comment

by:Dale Fye (Access MVP)
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 up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 

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 500 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

757 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now