Solved

Adding PK constraint after SELECT INTO fails.

Posted on 2008-10-08
8
598 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

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…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

821 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