Jeff Kish (jeff.kish@mro.com) writes:
Quote:
I need to add a new varchar(8) column that is not null to the primary key.
Ouch! That can be paintful, at least if there are foreign keys
referencing the table.
Quote:
abusing it).
Here is the code I'm using now.. it basically adds the column
'institution_id'
as not null along with a default.
Then I jump through a couple of hoops trying to get rid of the default.
Finally I setup the primary key again.
>
I can only feel I'm supposed to be maybe using a constraint column with
a name to do this easier/more properly.
Yes, if you name the constraint it's a little easier:
Quote:
set @dynamicsql = ' alter table institution_xref add institution_id
varchar(60) not null default ''' + @default_institution_id + ''' '
....not null CONSTRAINT my_temp_default DEFAULT ''' ...
Quote:
EXEC (@dynamicsql)
ALTER TABLE ... DROP CONSTRAINT my_temp_default
--
Erland Sommarskog, SQL Server MVP,
esquel@sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx