Solved

Adding PK constraint after SELECT INTO fails.

Posted on 2008-10-08
8
596 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
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 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

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

Suggested Solutions

Title # Comments Views Activity
convert in derived column 7 30
2016 SQL Licensing 7 41
Need help how to find where my error is in UFD 6 30
replace \ by - in select 4 21
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

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