Solved

Adding PK constraint after SELECT INTO fails.

Posted on 2008-10-08
8
602 Views
Last Modified: 2012-06-21
I need to create a new table from another table and then add a primary key to the new table.  In order to do that, the id column cannot be null.  So, I alter the id column to remove the nullability and then add the primary key.

If I run this as a batch, it fails saying "Cannot define PRIMARY KEY constraint on nullable column in table 'newTable'".  If I run this line-by-line in the query analyzer, it runs fine.  If I add a GO after the first ALTER TABLE statement, it runs fine as a batch.

Unfortunately, I need to run this as stored procedure, so I cannot use GO.  What do I need to change to let this run as a batch without submitting the first two statements as a batch, and then running the last statement?
SELECT * INTO newTable FROM oldTable
 
ALTER TABLE [newTable] ALTER COLUMN id int NOT NULL
 
ALTER TABLE [newTable] ADD CONSTRAINT [PK_newTable] PRIMARY KEY CLUSTERED
 (
   [id]
 ) ON [PRIMARY]

Open in new window

0
Comment
Question by:ChetOS82
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 22670711
maybe something like this:

create proc procname
as
begin
declare @x nvarchar(2000)

set @x = 'SELECT * INTO newTable FROM oldTable'
exec sp_executesql @x

 
set @x = 'ALTER TABLE [newTable] ALTER COLUMN id int NOT NULL'
exec sp_executesql @x
 
set @x = 'ALTER TABLE [newTable] ADD CONSTRAINT [PK_newTable] PRIMARY KEY CLUSTERED
 (
   [id]
 )'
exec sp_executesql @x
end
0
 
LVL 9

Expert Comment

by:jamesgu
ID: 22670746
can you change the nullability  of the old table?

ALTER TABLE [oldTable] ALTER COLUMN id int NOT NULL

before the select into statement?
0
 
LVL 18

Expert Comment

by:mdougan
ID: 22670783
Are you sure your first alter table statement succeeded?  I've been able to alter tables and add constraints inside of a proc and it worked OK.  If you have any nulls remaining in the ID column, that alter statement would fail so you might need to check @@sqlerror.  You could try adding a DEFAULT to the end of the statement.

0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 18

Author Comment

by:ChetOS82
ID: 22670846
"can you change the nullability  of the old table?"

No, it is an import table with raw data.  I clean it up in the SELECT INTO statement.

@mdougan: I can run each statement on its own without problems.
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 22670862
did you try my statement?
0
 
LVL 9

Accepted Solution

by:
jamesgu earned 500 total points
ID: 22670957
you may use the isnull statement to make the column not null in the new table,

for example,

create table oldTable (
id int,
name varchar(100)
)

go


SELECT isnull(id, 0) as id, name INTO newTable FROM oldTable
 
-- ALTER TABLE [newTable] ALTER COLUMN id int NOT NULL
 
ALTER TABLE [newTable] ADD CONSTRAINT [PK_newTable] PRIMARY KEY CLUSTERED
 (
   [id]
 ) ON [PRIMARY]


go

0
 
LVL 18

Author Closing Comment

by:ChetOS82
ID: 31504313
Worked like a charm, thanks!
0
 
LVL 18

Author Comment

by:ChetOS82
ID: 22671120
@chapmandew: No, because my SELECT INTO statement is just a proof of concept.  Adding the complexity of sp_executesql almost not worth it (especially escaping quotes, which is going to make it much harder for me to debug).
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

617 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