Solved

Creare Unique/Primary Key when using SELECT* INTO

Posted on 2014-10-21
7
444 Views
Last Modified: 2014-10-21
I have not been able to find a definitive solution ...

I am using Access 2010 to Pass a Make Table Query to SQL 2012 Express, which creates a new table based on a value in some column data and my query does work; however I always get an Access prompt to select a Primary/Unique Key.

Is there anyway to specify this Primary/Unique Key in the SELECT * INTO statement?

Here is my query ...

/****** Object:  Table [SQL_DB_Name].[dbo].[Raw_Data] Script Date: 10/21/2014 12:53:41 PM ******/
 
USE [SQL_DB_Name]
 
SELECT * INTO [ALL_Zero_ID_Rows]  FROM  [Raw_Data]
WHERE CAST([The_Number_in_This_Column] AS INTEGER) = 0;
0
Comment
Question by:d2beetle
7 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 40395382
No, you can't designate a PK or unique key as part of a SELECT ... INTO.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40395408
The low-brain solution here would be...

1.

Create the table, and include a column named id (or whatever floats your boat) that is an int identity(1,1) (Same as Access AutoNumber).  Set id as the primary key.

2.

Instead of SELECT INTO, do a DELETE FROM ALL_Zero_ID_Rows.  
TRUNCATE instead of DELETE works if you want to re-seed the id values starting at 1

3.

Do an insert, explicitly defining all rows, except for the id of course:

INSERT INTO ALL_Zero_ID_Rows (Col1, col2, col3) 
SELECT Col1, Col2, Col3 
FROM  [Raw_Data] 
WHERE CAST([The_Number_in_This_Column] AS INTEGER) = 0;

Open in new window

0
 
LVL 84
ID: 40395416
What is Raw_Data? If it's a table, then you could define a Primary Key on that, and the SELECT INTO should pick that up.
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 7

Expert Comment

by:slubek
ID: 40395423
What about:

SELECT IDENTITY(int,1,1) as ID, * 
INTO [ALL_Zero_ID_Rows]  FROM  [Raw_Data]
WHERE CAST([The_Number_in_This_Column] AS INTEGER) = 0; 

Open in new window


and later:

ALTER TABLE [ALL_Zero_ID_Rows]
  ADD CONSTRAINT PK_[ALL_Zero_ID_Rows]
  PRIMARY KEY (ID);

Open in new window

0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40395424
>> the SELECT INTO should pick [a PK] up. <<

I've never seen this happen.  What leads you to believe a PK constraint is copied into a table created using a SELECT ... INTO?
0
 
LVL 84
ID: 40395746
Not the Primary Key constraint, but rather the VALUE of the Primary Key ...
0
 

Author Closing Comment

by:d2beetle
ID: 40395985
Thanks, Scott.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

777 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