Solved

Creare Unique/Primary Key when using SELECT* INTO

Posted on 2014-10-21
7
408 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:
ScottPletcher 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
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.

 
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:ScottPletcher
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
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…

896 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