Solved

Adding PK constraint after SELECT INTO fails.

Posted on 2008-10-08
8
592 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
"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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 60

Expert Comment

by:chapmandew
Comment Utility
did you try my statement?
0
 
LVL 9

Accepted Solution

by:
jamesgu earned 500 total points
Comment Utility
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
Comment Utility
Worked like a charm, thanks!
0
 
LVL 18

Author Comment

by:ChetOS82
Comment Utility
@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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
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…
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
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.

743 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

17 Experts available now in Live!

Get 1:1 Help Now