Solved

Adding PK constraint after SELECT INTO fails.

Posted on 2008-10-08
8
594 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
  • 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
I have a large data set and a SSIS package. How can I load this file in multi threading?
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

910 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

25 Experts available now in Live!

Get 1:1 Help Now